Compare commits

..

51 Commits

Author SHA1 Message Date
Jakub Zych
ead8e42211 docs(19.8-06): record the go-ahead: release v0.4.2 2026-10-22 02:53:07 +02:00
Jakub Zych
cf061857a9 docs(19.8-06): release proposal for v0.4.2, awaiting go-ahead 2026-10-22 02:51:35 +02:00
Jakub Zych
8020b177ff docs(19.8-05): roadmap progress 5/6 plans 2026-10-22 02:46:02 +02:00
Jakub Zych
89c8325ac4 docs(19.8-05): complete review fixes plan 2026-10-22 02:45:15 +02:00
Jakub Zych
d311d029c3 docs(19.8): review fixes recorded, info findings captured as a todo
- CR-01..CR-03 and WR-01..WR-08 fixed with their commits (19.8-05)
- IN-01..IN-09 deferred to one pending todo (minor, framework)
2026-10-22 02:43:48 +02:00
Jakub Zych
1bf64b009f test(19.8): gate requires the review fixes' tests
- diary, bonfire, dev, summer and the Docker lagoon lists name the
  pinning tests of CR-01..CR-03 and WR-01..WR-08 under a Plan 05 comment
2026-10-22 02:38:33 +02:00
Jakub Zych
020fe838ea fix(lagoon): one migration set list for Migrate and PendingMigrations
- Migrate iterates migrationSets, so the startup summary, --pretend, the
  production question, the applied lines and migrate:status --json read
  the same list; each set keeps its error wording through a label
- migrate reports pending-before minus pending-after as its applied
  lines and result line, also when Migrate fails partway (then it prints
  what was applied and returns the error)
- TestMigrateAppliedLines (Docker) pins both (WR-07)
2026-10-22 02:36:19 +02:00
Jakub Zych
a9025c0d12 fix(bonfire): strip control characters from error blocks, process views and tables
- bonfire.Sanitize (additive), moved from diary's console channel: tab
  kept, CR and LF to a space, every other control character dropped
- applied before styling to every error-block line (message,
  explanation, Try lines, -v chain), process-view item names and errors,
  the current item of spinner and progress, failure lines, and table
  headers and cells in both layouts
- diary's console handler calls bonfire.Sanitize; README and the
  writing-commands page say so (WR-05)
2026-10-22 02:31:55 +02:00
Jakub Zych
39677f1819 fix(diary): retire replaced daily writers and serialise ApplyGlobals
- install and Reset retire the channels they replace: a retired daily
  writer appends a late record and closes the file again, so no record is
  lost and no descriptor stays open; diary.Close keeps reopening the
  installed writers on the next record
- ApplyGlobals holds the setup lock for its whole run (lock-free
  installLocked and setVerbosityLocked), so concurrent calls rebuild the
  channels once (WR-06)
2026-10-22 02:27:39 +02:00
Jakub Zych
c586c6f8a9 fix(diary): retry a daily file that failed to open
- a failed open no longer disables the writer for good: it skips records
  and tries again one minute later by its clock, or at the first record of
  a new date, whichever comes first
- the warning (still 'daily log file disabled') appears once per date
- README and the logging page describe the retry (WR-04)
2026-10-22 02:25:18 +02:00
Jakub Zych
9b62285654 fix(bonfire): mask secret flag values in the wrong-directory hint
- the Try line replaces the value of every long flag whose name contains
  password, passwd, secret, token or key with ***, in the --flag value and
  --flag=value forms; other arguments are unchanged
- README and the writing-commands page say so (WR-08)
2026-10-22 02:22:40 +02:00
Jakub Zych
fc7706dfff fix(dev): give serve the full shutdown grace before killing it
- dev.ShutdownGrace (internal, 15 seconds, longer than serve's 10-second
  drain) is the grace child.stop waits after SIGTERM, instead of 2 seconds
- summer's delegates use the same constant (appShutdownGrace)
- scaffolding docs say summer dev waits for serve's shutdown report (WR-03)
2026-10-22 02:20:47 +02:00
Jakub Zych
9c89705747 fix(summer): give delegated commands the terminal and keep their stderr apart
- appStreams hands the app binary summer's own stdout and stderr on a
  terminal, so it asks the production question, prints styled summaries
  and animates its widgets
- off a terminal the binary's stdout goes through summer's Output and its
  stderr to summer's stderr, so --json and -q output carry no log lines
- newAppCmd (unexported) takes the two writers; docs say the streams stay
  apart (CR-03)
2026-10-22 02:19:01 +02:00
Jakub Zych
323413c741 fix(summer): pass the app binary's exit code through and print its error once
- bonfire.WithExitCode (additive) makes an error carry the exit code
  bonfire.Execute returns; a code outside 1..255 gives 1
- runAppCmd, factored out of runAppBinary, returns
  Reported(WithExitCode(err, code)) when the binary exited with a code, so
  summer prints no second error block; a binary killed by a signal keeps
  summer's block and exit 1
- README, console introduction and upgrading notes say so (CR-02)
2026-10-22 02:17:09 +02:00
Jakub Zych
d151a252c5 fix(bonfire): restore the terminal when a live view is interrupted
- while a view runs, runLive relays SIGINT and SIGTERM (unless ignored);
  on a signal it kills the program, which shows the cursor and restores
  the terminal, unregisters the view, stops relaying and raises the same
  signal again, so the default action or the command's own handler still
  applies
- the generated main, bonfire.Execute's context and Ctrl+C for commands
  without a live view are unchanged
- TestLiveViewRestoresTerminalOnSignal pins both paths in a re-executed
  helper process (WR-01)
2026-10-22 02:14:10 +02:00
Jakub Zych
86e4e270d3 fix(bonfire): reroute only the owning console's lines to a live view
- the registered view keeps the console that started it; Println, Printf
  and status lines go above it only from that console, other consoles
  write to their own writers (bonfire.PrintAbove still reaches any view)
- runLive claims the single live slot by check and set under liveMu and
  reports when it could not, so the widget prints plain lines
- unregister, finish and wait run deferred, so a panic in work leaves no
  registration behind and still propagates (WR-02)
2026-10-22 02:10:48 +02:00
Jakub Zych
b077dc3598 fix(bonfire): keep the order of lines printed above a live view and drain them before it ends
- each PrintAbove block is one bubbletea print message sent straight to
  the program's queue under liveMu, so the event loop prints it in call
  order instead of a command goroutine racing it back
- runLive unregisters, sends finishMsg and waits under liveMu, so a late
  printer writes after the final frame and no line is lost
- TestPrintAboveOrderedNoLoss pins order and no loss for spinner,
  progress, process and a printer racing the finish (CR-01)
2026-10-22 02:07:45 +02:00
Jakub Zych
e8f5b1c89c docs(19.8): gap-closure plans for the code review findings and v0.4.2 2026-10-22 01:03:48 +02:00
Jakub Zych
dd123821d2 docs(19.8): code review of the console overhaul 2026-10-22 00:35:55 +02:00
Jakub Zych
0b1d7e5a79 docs(19.8): record code review disposition 2026-10-22 00:35:50 +02:00
Jakub Zych
6c2164ad38 docs(19.8-04): v0.4.1 released; roadmap progress 4/4 plans 2026-10-22 00:22:02 +02:00
Jakub Zych
8bacc22a85 docs(19.6.1): map phase to its g15office task 2026-10-22 00:14:37 +02:00
Jakub Zych
2ca68f897d docs(19.8-04): record the Task 5 reply (release v0.4.1) 2026-10-22 00:14:21 +02:00
Jakub Zych
295c1c1ee7 docs(19.6.1): create phase plan
Three plans per D-22: server side, SPA support (D-21), tests.
D-12 amended: a password set for another admin keeps their personal
API tokens, as admin:reset-password does. Research open questions
marked resolved.
2026-10-22 00:13:40 +02:00
Jakub Zych
a7431c34f0 docs(19.8-04): unit tests and gate executed, post-release fix awaiting go-ahead 2026-10-21 19:58:37 +02:00
Jakub Zych
72b8383c10 test(19.8): gate stages for hello, docs, plugins, apps, landing and docker
- hello: vet, every test and the console tests by name
- docs: the charm decision note, TestDocsTree, docs:build --check, no
  application names, examples on every summer command
- plugins: 15 swept repositories (vet, short, TestCommandsDocumented,
  GOWORK=off build) and the additive check on each sweep commit
- apps: go build, summer build leaves main.go unchanged, list and help
- landing: nuxt generate and its TAP suite, terminal and roadmap tests,
  sync-roadmap --check against origin/master
- docker: serve lifecycle, migration checklist flags, token JSON
- self-test: TAP detector, additive and NOT SWEPT rules on planted repos
- go stage: the new surf, conga, lagoon, flare, dev and summer names
2026-10-21 19:48:58 +02:00
Jakub Zych
2baec56420 test(cabana,flare,dev,summer): json, reported errors and dev serve
- Docker: admin:token:create and list --json, -q keeps the token
- flare generate and test-push failures print once and are Reported
- summer dev starts serve with the forwarded globals, reports rebuilds,
  failures and restarts, and stops serve with SIGTERM returning nil
- delegates carry two examples and say the global options pass through
2026-10-21 19:38:19 +02:00
Jakub Zych
6720caebd3 test(surf,conga,lagoon): summaries, reports, stats and checklist flags
- serve summary lines at -v and -q, records and HideOnTTY, DSN labels
- shutdown report: aborted requests fail the stop, a drained stop is nil
- request log: request_id cap, default on, off by config or env, never the
  path for matched, 404 and 405 routes, component http; route:list --json
- WorkerStats through runAttempt: done, failed, in flight, handed back
- queue:clear and lagoon's production question on a pseudo-terminal:
  no cancels with exit 1, --force, -n and other environments skip it
- Docker: PendingMigrations, migrate/rollback/deferred:purge --pretend,
  production runs off a terminal never read stdin, migrate:status --json
- key:generate --json and -q
2026-10-21 19:38:18 +02:00
Jakub Zych
fd5286884e test(docsite): command word with global flags
- the docs checker finds the command behind -v, -q, -qn, --env X, --env=X
- gate: bonfire and docsite names in the go stage, docsite in the race run
2026-10-21 19:22:00 +02:00
Jakub Zych
eeeee59ead test(bonfire): flags, exit codes and the widget matrix
- PeekGlobals table, Args round trip, hook and PeekGlobals agree (clusters too)
- reserved flags, duplicate names, Call runs no OnGlobals hook
- exit codes 0/1/2, Reported silence, scrubbed errors, wrong-directory hint
- widget matrix: not a terminal byte for byte, -q, --no-ansi, -n, terminal
- live models through Update and View and one program run into a buffer
- prompts under -n, Secret from piped stdin, ConfirmProceed, PrintAbove
2026-10-21 19:22:00 +02:00
Jakub Zych
7b58ad5fa5 test(bonfire): goldens for list, help and errors
- list, list --all, list --format=json and an --ansi styled list (D-13, D-16)
- admin:token:create help and a passthrough help; help <cmd> equals --help (D-14)
- error blocks: hint with {bin}, unknown command, -v chain on stderr (D-18)
- CheckDocs gaps in order and sorted; the version header and BuildVersion
- goldens render with color detection cleared; -update rewrites them
2026-10-21 19:22:00 +02:00
Jakub Zych
9641a1be59 refactor(bonfire): read build info through a package variable
BuildVersion calls readBuildInfo (debug.ReadBuildInfo) so tests can pin
the tagged, replaced and go.work cases. No behaviour change.
2026-10-21 19:14:55 +02:00
Jakub Zych
efb043e4ff fix(bonfire): PeekGlobals reads clustered shorthands like -qn and -vq
pflag accepts -qn as -q -n and -vvq as -v -v -q, but PeekGlobals only
read clusters made of v, so the generated main's pre-boot diary setup,
the error block of Execute and passthrough commands missed -q, -n and
-v when they were clustered. Read the cluster letter by letter and stop
at the first letter that is not a global shorthand.
2026-10-21 19:13:21 +02:00
Jakub Zych
db83a14056 test(conga,lagoon): River and GORM bridges
- River logs as component river at warn, conga at log.level, -v lets River info through
- an app-published logger wins for River and conga
- GORM: warn by default, info traces at -vvv, record-not-found ignored, parameterized SQL
- gate: the diary, sunscreen, conga and lagoon names in the go stage
2026-10-21 19:09:28 +02:00
Jakub Zych
24f4643283 test(diary): config, channels, rotation, redaction and slogtest
- config rules name their key; env strings overlay defaults; D-08 defaults
- component floors, verbosity 1-3, quiet on the console only
- daily rotation, retention, 0640 files, unwritable dir, lazy open, path confinement
- redaction on every driver, console control-character stripping and prefix
- loggers before Setup, Reset, HideOnTTY, 100 concurrent writers
- testing/slogtest for the console handler and the JSON pipeline
- sunscreen InstallDefault still installs a redacting logfmt handler
2026-10-21 19:09:28 +02:00
Jakub Zych
9b2fa6c324 test(19.8): phase gate with the smoke tests by name
- check-phase19.8.sh: go test -json detector, self-test on planted output
- go stage runs plans 1-3 smoke tests by name and the race run
- remaining stages stubbed so --all stays red
2026-10-21 18:58:30 +02:00
Jakub Zych
b906a31c36 docs(19.6.1): UI design contract 2026-10-21 18:33:12 +02:00
Jakub Zych
54a58ec7cc docs(19.8-03): roadmap progress 3/4 plans 2026-10-21 18:25:17 +02:00
Jakub Zych
2e4f85fc50 docs(19.8-03): complete the console sweep and v0.4.0 release plan 2026-10-21 18:24:59 +02:00
Jakub Zych
053c643527 docs(19.6.1): record post-research decisions D-19..D-22 2026-10-21 18:18:24 +02:00
Jakub Zych
77cf27eb24 docs(phase-19.6.1): add validation strategy 2026-10-21 18:16:34 +02:00
Jakub Zych
fff19e5e33 docs(19.6.1): research phase domain 2026-10-21 18:15:54 +02:00
Jakub Zych
a37bdbc4eb docs(19.6.1): capture phase context 2026-10-21 17:53:44 +02:00
Jakub Zych
a0ad2b5fa3 docs: insert phase 19.6.1 Backend Administrators screen; record 19.7 post-research decisions 2026-10-21 17:42:56 +02:00
Jakub Zych
39c0c42db7 docs(phase-19.7): add validation strategy 2026-10-21 17:38:00 +02:00
Jakub Zych
336ec2e0b6 docs(phase-19.7): add research 2026-10-21 17:38:00 +02:00
Jakub Zych
2ad7f539e3 docs(19.6): create phase plan 2026-10-21 16:33:26 +02:00
Jakub Zych
25ee333930 docs(phase-19.6): add pattern map 2026-10-21 15:58:39 +02:00
Jakub Zych
5c474b4a4f docs(phase-19.6): lock post-research decisions D-18 to D-20 2026-10-21 15:54:53 +02:00
Jakub Zych
c8cbcd5cc7 docs(phase-19.6): add research and validation strategy 2026-10-21 15:50:48 +02:00
Jakub Zych
c1f5ded59d docs: capture todo - Quiet plugin boot logs on console commands like --help 2026-10-21 15:34:02 +02:00
97 changed files with 16891 additions and 246 deletions

View File

@@ -46,5 +46,6 @@ A new phase, inserted or added, gets its task when it is added to `ROADMAP.md`.
| 14 | cwlaryop | 30 | 0rkcboo0 |
| 14.1 | v0j5lwvq | 31 | hgol7otp |
| | | 19.4.1 | g02hcroj |
| | | 19.6.1 | hioyvbxa |
Backlog phase 999.1 (newsletter) has no task until it is promoted into the roadmap.

View File

@@ -41,6 +41,7 @@ Decimal phases appear between their surrounding integers in numeric order.
- [ ] **Phase 19.4.1: Early public demo on demo.summercms.io** - sm-demo-app with today's shared plugins on rome: open registration, guided tour, journal, bm's presentation system, quizzes, payments; backend by request
- [x] **Phase 19.5: Social login in sm-user-plugin** - Google and Facebook login with the SPA one-time-code handoff (completed 2026-10-21)
- [ ] **Phase 19.6: Two-factor auth and device flow in sm-user-plugin** - TOTP, WebAuthn, email and recovery codes, trusted devices, device authorization
- [ ] **Phase 19.6.1: Backend Administrators screen** - Admins and roles CRUD in cabana, WinterCMS-style, with server-side privilege rules
- [ ] **Phase 19.7: Generic MCP module** - Opt-in MCP endpoint, plugin-declared tools filtered by permissions, one connector per person via OAuth and a verified account link
- [ ] **Phase 19.8: Console commands overhaul** - `diary` logging module, Artisan-grade global flags, help and `list` in `bonfire`, sweep of every command
- [ ] **Phase 20: Demo instance with every shared plugin** - Add the chat family (chat, channels, video calls, chatbots, forum, herald, friends) to the live demo from Phase 19.4.1
@@ -1395,15 +1396,49 @@ Plans:
3. Device authorization (`devices/initiate`, `devices/status/{token}`, `devices/authorize`, and the device list and revoke) works on the `DeviceAuthSession` model.
4. The models TwoFactorMethod, TwoFactorChallenge, TwoFactorRecoveryCode, TrustedDevice, WebAuthnCredential and DeviceAuthSession and their migrations are ported. The phase gets a security review. The new code has unit tests, delivered in the phase's last plan.
**Plans:** 0 plans
**Plans:** 4 plans
Plans:
- [ ] TBD (run /msd-plan-phase 19.6 to break down)
**Wave 1**
- [ ] 19.6-01-PLAN.md — Foundations: decision note for pquerna/otp, go-webauthn and boombuler/barcode, sm-user-plugin seams (login gates in Login with the restore after the gate, login and password-changed events, reset-2FA slot and permission), device authorization flow with the JTI followed through refresh, human package check (wave 1)
**Wave 2** *(blocked on Wave 1 completion)*
- [ ] 19.6-02-PLAN.md — New repo sm-mfa-plugin: TOTP tracer through the challenge, recovery and email codes with the D-18 hardening, trusted devices, settings screen, revocation on password change, admin reset service (wave 2)
**Wave 3** *(blocked on Wave 2 completion)*
- [ ] 19.6-03-PLAN.md — WebAuthn second factor and passwordless passkeys, social sign-in through the gate, demo 2FA screens in vue-demo-app, release go-ahead and publishing in D-17 order, adoption in sm-demo-app and sm-bm-app, fonoteka.go pointer and guard tests (wave 3)
**Wave 4** *(blocked on Wave 3 completion)*
- [ ] 19.6-04-PLAN.md — Unit tests for every decision, login-path invariant, PHP security test map, security review with the explicit D-14 section, scripts/check-phase19.6.sh, gated release of post-release fixes (wave 4)
### Phase 19.6.1: Backend Administrators screen (INSERTED)
**Goal:** cabana gets WinterCMS's Administrators screens, so backend admins and their roles are managed in the admin SPA instead of only through `admin:*` console commands. Kept short (1-2 plans): it reuses cabana's CRUD, permission field and record actions, and follows the sm-user-plugin Users/Groups screens and WinterCMS `Backend\Controllers\Users`/`UserRoles`.
**Depends on:** Phase 8 (cabana); 19.7 builds on it (the frontend-link record action lands on this screen)
**Repos:** `summercms.go`
**Requirements**: TBD
**Success Criteria** (what must be TRUE):
1. An admin with the new manage-administrators permission lists, creates, edits and deactivates backend users (login, email, name, role, password set/reset) and edits roles with the permission editor; existing cabana payloads and `admin:*` commands are unchanged.
2. Privilege rules hold on the server: only a superuser can create, edit or grant superuser; an admin cannot raise their own role or permissions; the last active superuser cannot be deactivated, demoted or deleted.
3. The record-action seam on the Administrators list is usable by plugins (19.7's link action), and unit tests cover the privilege rules.
**Plans:** 3 plans
Plans:
**Wave 1**
- [ ] 19.6.1-01-PLAN.md — server side: the pact action-extension seam, the internal summercms.cabana pseudo-plugin with the Administrators and Roles screens, every privilege rule in the save transaction (superuser hiding, ceilings, self-edit, outranking, last-superuser lock, revocation), lang keys and docs
**Wave 2** *(blocked on Wave 1 completion)*
- [ ] 19.6.1-02-PLAN.md — SPA support (D-21): per-record locked permission values and the field `trigger`, regenerated OpenAPI and schema.d.ts, rebuilt modules/boardwalk/dist
**Wave 3** *(blocked on Wave 2 completion)*
- [ ] 19.6.1-03-PLAN.md — unit, Postgres and SPA tests for every rule (incl. the held-lock last-superuser race), the seam and D-21, plus the check-phase19.6.1.sh gate
### Phase 19.7: Generic MCP module (INSERTED)
**Goal:** A SummerCMS application can expose its plugins' APIs as an opt-in MCP endpoint inside the binary. Plugins declare their tools in Go, and an editor connects one MCP client once to reach both frontend-user tools and the backend tools of their linked admin account.
**Depends on:** Phase 8; todos `2026-10-17-plugin-registered-frontend-permissions` and `2026-10-17-frontend-backend-account-link`
**Depends on:** Phase 8, Phase 19.6.1; todos `2026-10-17-plugin-registered-frontend-permissions` and `2026-10-17-frontend-backend-account-link`
**Repos:** `summercms.go`, `sm-user-plugin`
**Design:** `.planning/notes/2026-10-17-generic-mcp-module.md`
**Requirements**: TBD
@@ -1435,7 +1470,7 @@ Plans:
4. A sweep of all existing commands (framework modules and shared plugins) checks them against an Artisan feature checklist and fixes the gaps found, without changing command logic or breaking existing names and flags. The checklist covers confirmation with `--force` on destructive actions, `--pretend`/dry-run where it applies, progress bars and tables for bulk work, and `--json` output for scripting. Long-running commands such as `serve` and `queue:work` print a startup summary of what booted (environment, plugins, database and migrations state, queues, scheduler, mail, cache and realtime drivers, URLs) and report what they did on shutdown. One-shot commands end with a result line and a meaningful exit code.
5. The `diary` and `bonfire` READMEs, the docs console and logging pages, the summercms.io landing site and its roadmap presentation reflect the new console. The new code has unit tests, delivered in the phase's last plan.
**Plans:** 2/4 plans executed
**Plans:** 5/6 plans executed
Plans:
**Wave 1**
@@ -1445,10 +1480,12 @@ Plans:
- [x] 19.8-02-PLAN.md — `bonfire`: global flags, Artisan list and help, Examples/Hidden, JSON listing, CheckDocs, styled errors with hints and exit codes, duplicate-name rejection, SSU widgets on charm v2
**Wave 3** *(blocked on Wave 2 completion)*
- [ ] 19.8-03-PLAN.md — sweep of every command (checklist, hints, serve/queue:work/dev summaries and reports, http request log), docs, landing and roadmap deck; release checkpoint, then plugin commits and app pointer bumps
- [x] 19.8-03-PLAN.md — sweep of every command (checklist, hints, serve/queue:work/dev summaries and reports, http request log), docs, landing and roadmap deck; release checkpoint, then plugin commits and app pointer bumps
**Wave 4** *(blocked on Wave 3 completion)*
- [ ] 19.8-04-PLAN.md — unit and golden tests for all new code, `scripts/check-phase19.8.sh` gate across repos, post-release fixes go-ahead
- [x] 19.8-04-PLAN.md — unit and golden tests for all new code, `scripts/check-phase19.8.sh` gate across repos, post-release fixes go-ahead
- [x] 19.8-05-PLAN.md — gap closure: fixes for the code review's critical and warning findings (live-view output order, summer delegates' exit codes and terminal, Ctrl+C cursor, dev shutdown grace, diary file retry, escape stripping, migration sets), each with a pinning test
- [ ] 19.8-06-PLAN.md — gap closure: v0.4.2 release go-ahead, tag and changelog, demo roadmap sync, full gate
### Phase 20: Demo instance with every shared plugin
@@ -1551,8 +1588,9 @@ Phases execute in numeric order: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 →
| 19.4.1. Early public demo on demo.summercms.io | 5/5 | In Progress| |
| 19.5. Social login in sm-user-plugin | 5/5 | Complete | 2026-10-21 |
| 19.6. Two-factor auth and device flow in sm-user-plugin | 0/TBD | Not started | - |
| 19.6.1. Backend Administrators screen (INSERTED) | 0/TBD | Not started | - |
| 19.7. Generic MCP module | 0/TBD | Not started | - |
| 19.8. Console commands overhaul (INSERTED) | 2/4 | In Progress| |
| 19.8. Console commands overhaul (INSERTED) | 5/6 | In Progress| |
| 20. Demo instance with every shared plugin | 0/TBD | Not started | - |
| 30. Płytarium cutover | 0/TBD | Not started | - |
| 31. Chat polish against WavePath clients | 0/TBD | Not started | - |

View File

@@ -0,0 +1,420 @@
---
phase: 19.6-two-factor-auth-and-device-flow-in-sm-user-plugin
plan: 01
type: execute
wave: 1
depends_on: []
files_modified:
- .planning/notes/2026-10-21-mfa-otp-webauthn.md
- .planning/research/STACK.md
- ../fonoteka.go/plugins/golem15/user/classes/login_gate.go
- ../fonoteka.go/plugins/golem15/user/classes/events.go
- ../fonoteka.go/plugins/golem15/user/classes/two_factor_admin.go
- ../fonoteka.go/plugins/golem15/user/classes/device_auth.go
- ../fonoteka.go/plugins/golem15/user/controllers/login_gates.go
- ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go
- ../fonoteka.go/plugins/golem15/user/controllers/registration.go
- ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go
- ../fonoteka.go/plugins/golem15/user/controllers/users/config_form.yaml
- ../fonoteka.go/plugins/golem15/user/controllers/devices.go
- ../fonoteka.go/plugins/golem15/user/models/device_auth_session.go
- ../fonoteka.go/plugins/golem15/user/updates/202610210002_create_device_auth_sessions.go
- ../fonoteka.go/plugins/golem15/user/admin_permissions.go
- ../fonoteka.go/plugins/golem15/user/plugin.go
- ../fonoteka.go/plugins/golem15/user/routes.go
- ../fonoteka.go/plugins/golem15/user/config/config.yaml
- ../fonoteka.go/plugins/golem15/user/lang/en/lang.yaml
- ../fonoteka.go/plugins/golem15/user/lang/pl/lang.yaml
- ../fonoteka.go/plugins/golem15/user/login_gate_smoke_test.go
- ../fonoteka.go/plugins/golem15/user/password_changed_smoke_test.go
- ../fonoteka.go/plugins/golem15/user/admin_registration_test.go
- ../fonoteka.go/plugins/golem15/user/reset_two_factor_smoke_test.go
- ../fonoteka.go/plugins/golem15/user/device_flow_smoke_test.go
- ../fonoteka.go/plugins/golem15/user/device_revoke_smoke_test.go
- ../fonoteka.go/plugins/golem15/user/README.md
autonomous: false
requirements: [SC-2, SC-3, SC-4, D-02, D-03, D-04, D-06, D-07, D-11, D-12a, D-12b, D-12c, D-15, D-16, D-17, D-18]
estimate:
tokens: 290000
raw_tokens: 290000
tasks: 5
confidence: low
must_haves:
truths:
- "D-04: a decision note .planning/notes/2026-10-21-mfa-otp-webauthn.md names github.com/pquerna/otp v1.5.0, github.com/go-webauthn/webauthn v0.18.2 and github.com/boombuler/barcode v1.1.0 (QR, with the plugin's own SVG writer), the measured +2,269,184-byte binary delta, the nine newly linked modules, the go-viper/mapstructure v2.5.0 bump, the clean govulncheck run, the D-08 JSON-shape deviation and the blocking human package check; .planning/research/STACK.md lists the three pins; the note is committed before any go.mod requires one of them, and no go.mod in sm-user-plugin, the framework or fonoteka.go ever requires them."
- "D-02: sm-user-plugin publishes a *classes.LoginGates registry in Register; Login consults it after CheckAndRecordLogin(..., true) and the bcrypt rehash and before the soft-delete restore and the mint; a gate result answers its own status and JSON body (HTTP 200 two_factor_required for a challenge) and no token is minted; a gate error answers the opaque 500; an empty registry leaves Login's responses byte-identical to v0.1.5; JWT refresh never consults the gate."
- "D-18 (A10): with a gate that challenges, a soft-deleted account stays soft-deleted and no reactivate mail is sent; the restore and the reactivate mail run only after the gate passed."
- "D-02, D-05 support: every successful password login fires classes.LoginEvent (Source password) exactly once, after the token is minted; a challenged login fires none; controllers.FireLoginEvent and controllers.CheckLoginGates are exported for sm-mfa-plugin and sm-sociallogin-plugin."
- "D-07: classes.PasswordChangedEvent fires inside the password-write transaction from ResetPassword (Source reset), ChangePassword (Source change) and the admin password set (Source admin), and a listener error rolls the write back; the bcrypt rehash on login and RegisterUser fire none."
- "D-15: the user plugin registers the backend permission golem15.users.reset_two_factor (no default role) and a reset_two_factor record action on the Users screen with Danger true; Applies is false when no classes.TwoFactorAdmin service is published, so applications without sm-mfa-plugin never show it; Run calls the published service's Reset inside cabana's transaction."
- "D-03, D-11, D-16: POST /_user/api/v1/devices/initiate, GET devices/status/{token}, POST devices/authorize, GET devices and DELETE devices/{id} (id [0-9]+) live in the user-api group; the session row is golem15_user_device_auth_sessions with PHP's column names, a 64-character token stored as its SHA-256 hex (D-06), an XXXX-XXXX short code over 23456789ABCDEFGHJKMNPQRSTUVWXYZ, a 5-minute TTL, PHP's status set pending|confirmed|used|expired|revoked and an added jwt_jti column; auth_url comes from golem15.user.device_auth.auth_url (default {frontend_url}/device/scan/{token})."
- "D-12a: the first poll after confirmation answers {success:true, data:{status:'confirmed', user_id, message:'Device authorized - complete login', token, user}} through one guarded confirmed-to-used UPDATE; of concurrent polls exactly one receives a token and the others answer status used; used and revoked sessions never turn into expired."
- "D-12b: DELETE devices/{id} marks the row revoked and adds its jwt_jti to the JWT blacklist; Refresh moves the row's jwt_jti from the old token's jti to the new one, so a device that refreshed before the revoke is signed out too."
- "D-12c: POST devices/authorize needs only a valid, non-blacklisted bearer JWT (PHP 401 body {\"error\":\"Unauthorized\"} otherwise) and binds a pending session with no user to the caller through one guarded pending-to-confirmed UPDATE; there is no second-factor re-check."
- "D-17 support: every change to sm-user-plugin is additive (no route, payload, table, column, migration ID or config key of v0.1.5 changes); fonoteka.go, sm-sociallogin-plugin and sm-oauthserver-plugin compile against the checkout; nothing is pushed or tagged in this plan."
artifacts:
- path: ".planning/notes/2026-10-21-mfa-otp-webauthn.md"
provides: "D-04 decision note naming the three pinned libraries and the human package check"
contains: "go-webauthn/webauthn v0.18.2"
- path: "../fonoteka.go/plugins/golem15/user/classes/login_gate.go"
provides: "LoginAttempt, LoginGateResult, LoginGate, LoginGates"
contains: "type LoginGate interface"
- path: "../fonoteka.go/plugins/golem15/user/controllers/devices.go"
provides: "device authorization handlers"
contains: "func DeviceStatus"
- path: "../fonoteka.go/plugins/golem15/user/updates/202610210002_create_device_auth_sessions.go"
provides: "golem15_user_device_auth_sessions table"
contains: "golem15_user_device_auth_sessions"
- path: "../fonoteka.go/plugins/golem15/user/login_gate_smoke_test.go"
provides: "TestLoginGatesSmoke and TestLoginGateSmoke"
contains: "TestLoginGateSmoke"
key_links:
- from: "../fonoteka.go/plugins/golem15/user/controllers/api_controller.go"
to: "../fonoteka.go/plugins/golem15/user/classes/login_gate.go"
via: "Login calls CheckLoginGates between CheckAndRecordLogin and the restore and mint"
pattern: "CheckLoginGates\\("
- from: "../fonoteka.go/plugins/golem15/user/controllers/api_controller.go"
to: "golem15_user_device_auth_sessions.jwt_jti"
via: "Refresh hands the device row's jti over to the refreshed token"
pattern: "HandOverDeviceJTI"
- from: "../fonoteka.go/plugins/golem15/user/controllers/devices.go"
to: "bouncer.BlacklistStore"
via: "DeviceDestroy blacklists the row's jwt_jti"
pattern: "Lookup\\[bouncer.BlacklistStore\\]"
- from: "../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go"
to: "../fonoteka.go/plugins/golem15/user/classes/two_factor_admin.go"
via: "reset_two_factor looks up the published TwoFactorAdmin per request"
pattern: "Lookup\\[classes.TwoFactorAdmin\\]"
prohibitions:
- statement: "No JWT, device token, short code, challenge token or password is ever written to a log line or an error string by the code this plan adds."
status: unresolved
verification: judgment
- statement: "sm-user-plugin changes are additive only: no existing route, response body, table, column, migration ID or config key changes; the only new migration is the device-session table (core plugin contract, 19.4 D-05)."
status: unresolved
verification: judgment
- statement: "No go.mod in sm-user-plugin, summercms.go or fonoteka.go requires pquerna/otp, go-webauthn or boombuler/barcode, and none is installed anywhere before the Task 5 reply."
status: unresolved
verification: judgment
- statement: "Nothing is pushed or tagged and no remote repository is created in this plan (plan 03 asks for the go-ahead)."
status: unresolved
verification: judgment
- statement: "Framework code and plugin READMEs name no consuming application (neutral wording: the application)."
status: unresolved
verification: judgment
---
<!-- planner-discipline-allow: pquerna/otp -->
<!-- planner-discipline-allow: go-webauthn -->
<!-- planner-discipline-allow: boombuler -->
## Phase Goal
ROADMAP Goal (not in user-story format; MVP mode would take a story from `/msd mvp-phase 19.6`, which was not run, so no story is invented here, as in 19.5): `sm-user-plugin` has the PHP user plugin's second factors and device login: TOTP, WebAuthn, email codes, recovery codes and trusted devices, a challenge step during login, and QR/code device authorization. CONTEXT D-01 moves the second factors into a new shared plugin, `sm-mfa-plugin`; D-03 keeps the device flow in `sm-user-plugin`.
This plan's slice (plan 1 of 4, wave 1, "Foundations"): the decision note for the three libraries lands first. Then sm-user-plugin gets its additive seams: the login-gate registry wired into Login (the tracer), the login and password-changed events, the admin reset slot, and the whole device authorization flow with the JTI hand-over on refresh. The plan ends with the blocking human package check, so plan 02 can install the libraries.
## Spec-less probe fallback
spec-less probe fallback skipped: phase has no requirement IDs to probe (visible skip). No SPEC.md exists and ROADMAP lists "Requirements: TBD". `must_haves` derive from the ROADMAP success criteria SC-1..SC-4 and CONTEXT decisions D-01..D-20 (with D-12a, D-12b, D-12c); `requirements` lists those IDs for traceability.
<assumption_delta_decision>
- Primary noun for authentication: before this phase a sign-in was "password (or social identity) → token". After it, it is "first factor (password, social identity) → optional second factors (TOTP, email code, recovery code, WebAuthn) → token", plus a passkey as an alternative first factor that satisfies the second factor by itself (D-14).
- Decision: **add-alongside**. Password login stays the primary path and its contract is unchanged when no gate is registered (D-02). Second factors are layered on through a registry of gates that sm-mfa-plugin fills; applications without sm-mfa-plugin behave exactly as before (D-01).
- Accepted debt: token mints that do not consult the gate, each by decision: JWT refresh (D-02, PHP), POST /register and activate-by-code (a new or not-yet-activated account cannot have 2FA yet), sociallogin's action=link (the caller is already signed in), the device poll (D-12c: the authorizing session already passed 2FA), the challenge completion and the passwordless passkey login (they are the second factor), and fonoteka's onboarding IssueToken (fonoteka does not mount mfa, D-17). Social sign-in consults the gate only when the mfa setting turns it on (D-09, default skip).
- Invariant test (plan 04): `TestEveryLoginPathIsGated` in sm-mfa-plugin drives every first-factor path (password; social with the toggle on) for a user with TOTP enabled and asserts a challenge and no token, and asserts that passwordless issues a token only for a user-verified discoverable passkey and the device poll only after an authorize by a bearer JWT.
</assumption_delta_decision>
## Phase source audit (all four plans)
| SOURCE | ID | Item | Plan | Status |
|---|---|---|---|---|
| GOAL | — | Second factors, challenge step and device login of the PHP user plugin | 01-03 | COVERED |
| ROADMAP | SC-1 | TOTP, WebAuthn, email codes, recovery codes, trusted devices; WebAuthn decision note first | 01 (note), 02, 03 | COVERED |
| ROADMAP | SC-2 | Challenge step behind the 2fa-verify bucket | 01 (gate), 02 | COVERED |
| ROADMAP | SC-3 | Device authorization on DeviceAuthSession | 01 | COVERED |
| ROADMAP | SC-4 | Six models and migrations, security review, unit tests last | 01, 02, 03, 04 | COVERED |
| CONTEXT | D-01 | Separate sm-mfa-plugin requiring the user plugin | 02, 03 (repo creation) | COVERED |
| CONTEXT | D-02 | Login-gate registry | 01 | COVERED |
| CONTEXT | D-03 | Device flow in sm-user-plugin | 01 | COVERED |
| CONTEXT | D-04 | Libraries, decision note, QR pick | 01, 02, 03 | COVERED |
| CONTEXT | D-05 | PHP paths, fields, shapes, bucket, TTLs, events | 02, 03 | COVERED |
| CONTEXT | D-06 | SHA-256 tokens at rest, encrypted TOTP secret, slow hash | 01, 02 | COVERED |
| CONTEXT | D-07 | Revocation on password change and last-method disable | 01, 02 | COVERED |
| CONTEXT | D-08 | Standard WebAuthn JSON shapes | 03 | COVERED |
| CONTEXT | D-09 | Social login through the challenge behind a setting | 02 (setting), 03 | COVERED |
| CONTEXT | D-10 | Trusted devices | 02 | COVERED |
| CONTEXT | D-11, D-12a, D-12b, D-12c | Device flow contract, one-time JWT, revoke, authorize | 01 | COVERED |
| CONTEXT | D-12 | Settings screen | 02 | COVERED |
| CONTEXT | D-13 | No child exemption | 02 | COVERED |
| CONTEXT | D-14 | Passwordless passkey routes and security review | 03, 04 | COVERED |
| CONTEXT | D-15 | Admin Reset 2FA | 01 (slot), 02 (service) | COVERED |
| CONTEXT | D-16 | Six tables with PHP names | 01, 02, 03 | COVERED |
| CONTEXT | D-17 | Adoption in demo and bm, fonoteka guard test | 03 | COVERED |
| CONTEXT | D-18 | A6, A8, A9, A10 hardening | 01 (A10), 02 | COVERED |
| CONTEXT | D-19 | First recovery set returned | 02 | COVERED |
| CONTEXT | D-20 | Demo 2FA UI in vue-demo-app | 03 | COVERED |
| RESEARCH | Q7 | Refresh lineage gap (JTI hand-over) | 01 | COVERED |
| RESEARCH | Pitfalls 1-21 | Throttle reset, replay, races, RP ID, used-not-expired, name bounds, table growth, Where binding, postcard ownership, settings types, boot without DB | 01-03 | COVERED |
| RESEARCH | Validation Architecture | check-phase19.6.sh, PHP map, decision-to-test map | 04 | COVERED |
Deferred and not planned (CONTEXT Deferred Ideas): per-group exemptions, notification mails on 2FA changes, the Winter import of the 2FA and device tables.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- Gate identifiers (CONTEXT discretion): `classes.LoginAttempt{User *models.User; Source string; Fields map[string]any; Request *http.Request}`; source constants `classes.LoginSourcePassword` ("password"), `LoginSourceSocial` ("social"), `LoginSourcePasskey` ("passkey"), `LoginSourceTwoFactor` ("two_factor"), `LoginSourceDevice` ("device"); `classes.LoginGateResult{Status int; Body map[string]any}`; `classes.LoginGate` with one method `CheckLogin(ctx context.Context, a LoginAttempt) (*LoginGateResult, error)`; `classes.LoginGates` with `NewLoginGates()`, `Add(LoginGate)` and `Check(ctx, LoginAttempt) (*LoginGateResult, error)`; `controllers.CheckLoginGates(ctx, app, a)`. A result carries a status so sm-mfa-plugin can refuse a new challenge with a 401 when the D-18 A6 failure cap is reached; a challenge is Status 200.
- Login order after this plan (D-02 refined by D-18 A10): lookup, throttle check, password check, `CheckAndRecordLogin(..., true)`, bcrypt rehash (before the gate, so users who are always challenged still get rehashed; a rehash is not a password change, Pitfall 3), gate, soft-delete restore with the reactivate mail, `TouchLastSeen`, mint, `LoginEvent`, response.
- Event names: `classes.LoginEventName = "golem15.user.login"` (PHP's name, D-05 "then the user login event"); `classes.PasswordChangedEventName = "user.PasswordChanged"` (Go-originated, the style of `user.Created`); sources `classes.PasswordChangeSourceReset`, `PasswordChangeSourceChange`, `PasswordChangeSourceAdmin`.
- Device routes authenticate inside the handlers, as PHP's controller does (its routes carry no jwt.auth), with PHP's 401 body `{"error":"Unauthorized"}`; the bearer token is parsed in the handler anyway because `is_current` compares its jti with the row's `jwt_jti` (PHP compared a CMS session id).
- The device table keeps PHP's column name `token` for the SHA-256 hex (64 characters fit PHP's VARCHAR(64), RESEARCH Q4), with Go field `TokenHash`; `session_id` is kept (always NULL) so a Winter import stays a near-straight copy (D-16).
- `device_name` in `GET devices` uses PHP's model accessor (stored name, else the user-agent parse: iPhone, iPad, Android Device, Windows PC, Mac, Linux PC, Unknown Device). PHP's list read a nonexistent `device_name_attribute` and always answered null; the key and type stay, the value is filled (recorded deviation).
- The device hand-over fires `LoginEvent` with Source device (PHP's CMS component logged the device in through Auth).
- Re-authorizing a session that the same caller already confirmed answers PHP's success body (PHP's `confirm()` returned false silently); any other non-pending session answers 400 `Invalid or expired code`.
- New config keys (additive): `golem15.user.frontend_url` (default empty, meaning app.url), `golem15.user.device_auth.auth_url` (default `{frontend_url}/device/scan/{token}`), `golem15.user.device_auth.ttl_minutes` (default 5).
- Migration ID `202610210002_create_device_auth_sessions` (after the existing `202610210001_add_users_email_verified_at`).
- Reversibility: D-02 and D-15 add exported seams other plugins implement (costly, flagged); the device table becomes one-way only when released, which plan 03's go-ahead gates. D-01, D-08 and D-14 were chosen by the user in CONTEXT, so no checkpoint re-asks them.
- Concurrency with Phase 19.8: its plan 03 Task 8 may sweep this same sm-user-plugin checkout and only runs when the checkout is clean and in sync with origin. This plan therefore starts only when the checkout is clean and `origin/master..master` is empty (Task 1 precondition), and it never pushes.
- Context budget (estimate 290k, 5 tasks; the project's lean-planning rule fixes four plans): the first agent runs Tasks 1-3; if its context runs low after Task 3, it writes `19.6-01-SUMMARY.md` as a draft (`Status: draft (hand-off after Task 3)`, commits with hashes, test names, NOT RUN notes) and a fresh agent runs Task 4 and the Task 5 checkpoint from this plan, the draft and the repositories on disk.
## Artifacts this phase produces (this plan)
- Decision note `.planning/notes/2026-10-21-mfa-otp-webauthn.md`; three rows in `.planning/research/STACK.md` Supporting Libraries.
- sm-user-plugin (`git.golem15.com/golem15/sm-user-plugin`):
- classes: `LoginAttempt`, `LoginGateResult`, `LoginGate`, `LoginGates`, `NewLoginGates`, `(*LoginGates).Add`, `(*LoginGates).Check`; constants `LoginSourcePassword`, `LoginSourceSocial`, `LoginSourcePasskey`, `LoginSourceTwoFactor`, `LoginSourceDevice`; `LoginEvent{User, Source}`, `LoginEventName`; `PasswordChangedEvent{User, Tx, Source}`, `PasswordChangedEventName`, `PasswordChangeSourceReset`, `PasswordChangeSourceChange`, `PasswordChangeSourceAdmin`; `TwoFactorAdmin` interface (`HasTwoFactor(ctx context.Context, tx *gorm.DB, userID uint) (bool, error)`, `Reset(ctx context.Context, tx *gorm.DB, user *models.User) error`); `PermissionResetTwoFactor` (`golem15.users.reset_two_factor`); device helpers `DeviceShortCodeAlphabet`, `NewDeviceToken() (string, error)`, `NewDeviceShortCode() (string, error)`, `NormalizeDeviceShortCode(s string) (string, bool)`, `DeviceAuthURL(app *backpack.App, token string) string`, `DeviceAuthTTL(app *backpack.App) time.Duration`, `DeviceDisplayName(stored *string, userAgent string) string`, `HandOverDeviceJTI(ctx context.Context, db *gorm.DB, oldJTI, newJTI string) error`.
- controllers: `CheckLoginGates(ctx context.Context, app *backpack.App, a classes.LoginAttempt) (*classes.LoginGateResult, error)`, `FireLoginEvent(ctx context.Context, app *backpack.App, user *models.User, source string) error`, `FirePasswordChangedEvent(ctx context.Context, app *backpack.App, user *models.User, tx *gorm.DB, source string) error`, handler factories `DeviceInitiate`, `DeviceStatus`, `DeviceAuthorize`, `DeviceIndex`, `DeviceDestroy`; record action `reset_two_factor` on the Users screen.
- models: `DeviceAuthSession` (table `golem15_user_device_auth_sessions`), constants `DeviceStatusPending`, `DeviceStatusConfirmed`, `DeviceStatusUsed`, `DeviceStatusExpired`, `DeviceStatusRevoked`.
- migration `202610210002_create_device_auth_sessions`.
- routes (user-api group, throttle:user-api): `POST /_user/api/v1/devices/initiate`, `GET /_user/api/v1/devices/status/{token}`, `POST /_user/api/v1/devices/authorize`, `GET /_user/api/v1/devices`, `DELETE /_user/api/v1/devices/{id}`.
- config keys `golem15.user.frontend_url`, `golem15.user.device_auth.auth_url`, `golem15.user.device_auth.ttl_minutes`; permission `golem15.users.reset_two_factor`; lang keys `golem15.user::lang.plugin.reset_two_factor`, `golem15.user::lang.users.reset_two_factor`, `.reset_two_factor_confirm`, `.reset_two_factor_success`.
- smoke tests `TestLoginGatesSmoke`, `TestLoginGateSmoke`, `TestPasswordChangedEventSmoke`, `TestResetTwoFactorSlotSmoke`, `TestDeviceCodesSmoke`, `TestDeviceFlowSmoke`, `TestDeviceRevokeSmoke`.
<objective>
Record the library decision, then give sm-user-plugin every additive seam the phase builds on: the login gate (tracer), the login and password-changed events, the admin reset slot and the complete device authorization flow with JTI tracking through refresh; stop for the human package check.
Purpose: D-02, D-03, D-04, D-06 (device tokens), D-07 (hook), D-11, D-12a, D-12b, D-12c, D-15 (slot), D-16 (device table), D-17 (additive, fonoteka compiles), D-18 A10; SC-2 (the gate), SC-3 (device authorization), SC-4 (DeviceAuthSession and its migration).
Output: one planning-doc commit (note and STACK.md), four sm-user-plugin commits each with its README change, the recorded human reply.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-CONTEXT.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-RESEARCH.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-PATTERNS.md
Repos and concurrency: this plan writes in summercms.go (planning docs only) and in the sm-user-plugin repository checked out at ../fonoteka.go/plugins/golem15/user (a submodule of fonoteka.go with its own git; use `git -C ../fonoteka.go/plugins/golem15/user`). Never stage anything in the fonoteka.go superproject (its feedback and golem pointer moves belong to other sessions; plan 03 commits the user pointer). Phase 19.8 plan 03 may sweep the same checkout: before Task 1 run `git -C ../fonoteka.go/plugins/golem15/user fetch origin`, then `status --porcelain` and `log --oneline origin/master..master`; both must be empty, else stop and report. fonoteka.go's own fonoteka-plugin tests `TestNoDeferredRoutesResolve` (bans /devices) and the user-model count in `plugins/golem15/fonoteka/classes/hidden_marshal_test.go` turn red once the device routes and model exist in this checkout; that is expected until plan 03's fonoteka pointer-bump commit updates them (D-17), so this plan's verify commands run the user plugin's suite and fonoteka's build, not the fonoteka plugin's suite. No co-author tags; stage by explicit path; planning docs and code in separate commits; README in the same commit as the API it describes. Keep `go vet` and `go test -short` of the user plugin green at every commit. Docker-backed tests skip under -short; run them on a Docker host (through `sudo -u golem` if the socket is denied); Phase 19.8 found this host's container networking unreachable, so a Docker test that cannot run is recorded as NOT RUN in the SUMMARY, never as passed.
PHP source of truth (read-only): /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/ApiController.php (login challenge :55-76, refresh :149-174, device list :1175-1202, revoke :1208-1227, authorize :1233-1270, initiate :1277-1308, status :1315-1347), models/DeviceAuthSession.php (status constants :86-90, short code :142-160, findValidShortCode :168, findValidToken :204, confirm :262, device name accessor :337-361, getAuthUrl :388), updates/v2.1.0-v2.7.1 device migrations, routes.php :191-225. RESEARCH Q4 quotes them; the PHP files break ties. The only device-flow client is /media/nvme/dev/golem15/queststream.online/vue-queststream-app/composables/useDeviceAuth.ts (reads `response.data || response`, polls every 2 s, stops on expired or confirmed).
<interfaces>
sm-user-plugin v0.1.5 (paths relative to ../fonoteka.go/plugins/golem15/user):
- controllers/api_controller.go: `Login` :47-127 (readFields :53, restore block :87-93, `CheckAndRecordLogin(ctx, gdb, user, ip, true)` :94-101, rehash :102-110, `TouchLastSeen` :112, `mintFor` :115, final `writeJSON(w, http.StatusOK, map[string]any{"token": token, "user": payload})` :125); `Refresh` :179-213 (`bouncer.Refresh(secret, raw, refreshTTL(app), bl, blacklistGrace(app), requestURL(r))` :194, best-effort TouchLastSeen block :204-210); `ResetPassword` password write :327-343; `ChangePassword` :580-597; `authenticate` :795-825 (VerifyClaims, NewJWTGuard(...).Authenticate, user load); `sessionDeps` :827; `mintFor` :877-884 (discards the jti); `RemoteIP` :1077; `writeUnauthorized` :1104 (`{"error": true, "message": "Unauthorized"}`, not PHP's device body); `writeValidation` :946; `Logout` blacklist call (`bl.Add(r.Context(), jti, exp, time.Now())`).
- controllers/registration.go: `FinishLogin` :99, `FireDeactivatingEvent` :166-171 (pattern for the new Fire helpers), `IssueToken` :176, `APIArray` :189.
- controllers/users_admin_controller.go: `usersAdminController{app func() *backpack.App}` :37; admin password set in FormBeforeUpdate :238-247 (`changed, err := c.storePassword(ctx, user)`); `AdminRecordActions` :665-730 (the `action(...)` closure, `cabana.TxFromContext`); `requestDB(ctx, c.db)` in controllers/request_db.go.
- controllers/users/config_form.yaml :17 `recordActions: [activate, unban, unsuspend]`; controllers/api_tokens.go (List :66-85 with `wire.Slice`, Destroy :88-111 with scoped `Where("user_id = ?", ...)`).
- classes/events.go (event types and name constants, DeactivatingEvent :59-67); classes/oauth_providers.go (seam doc with the explicit-type Publish warning); classes/api_tokens.go `HashAPIToken` :96 (SHA-256 hex, the D-06 scheme); classes/privileged.go :16 `PermissionManagePrivilegedGroups`.
- plugin.go: `Register` :62-65 (publish the registry here), `Boot` (typed `app.Publish[bouncer.BlacklistStore](bl)`), `Buckets` user-api :150-171; routes.go :17-37 (`g.Delete("/tokens/{id}", ...)` then `g.Where("id", "[0-9]+")`); admin_permissions.go (manage_privileged_groups without Roles); config/config.yaml; updates/202610040001_create_api_tokens.go (raw-SQL migration with `Register(...)` in init); models/api_token.go (model style).
- tests: session_test.go `sessionApp` :446, `insertUser` :382, `loginToken` :364, `bearer` :405, `TestLogin` :54, `TestRefresh` :225; admin_harness_test.go `newAdminEnv` :131, `(*adminEnv).admin` :197, `call` :242, `expect` :371, `mails` :325.
- Framework: `bouncer.Mint(secret, sub, issuerURL string, ttl time.Duration) (token, jti string, err error)` (mint.go:31), `bouncer.VerifyClaims(token, secret) (sub string, iat, exp time.Time, jti string, err error)` (jwt.go:194), `bouncer.BlacklistStore.Add(ctx, jti string, expiresAt, validUntil time.Time) error` (blacklist.go:16), `app.Events.Fire(ctx, &T{})`, `app.Events.Listen[*T](owner, fn)` (festival), `wire.Time`, `wire.Slice`, `cabana.TxFromContext`, `pact.AdminRecordAction{Name, Label, Confirm, Permissions, Danger, Applies, Run}`.
- RESEARCH Code Examples 1 (registry), 7 (device hand-over), 8 (refresh hand-over); Patterns 1, 2, 4, 5; Pitfalls 1, 2, 3, 13, 14, 15, 16, 17, 21.
- Decision-note structure: .planning/notes/2026-10-20-sociallogin-goth.md (Decision, Date, Status, Context, Decision bullets, measurements, rejected alternatives, Related dependencies, Package legitimacy, Consequences).
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: the library note lands first, then a password login passes through a registered gate end to end: no gate keeps the old answer, a challenging gate replaces the token with its body and leaves a deactivated account untouched, an erroring gate fails closed, and a successful login fires the login event</name>
<precondition>`git -C ../fonoteka.go/plugins/golem15/user status --porcelain` prints nothing and, after `git -C ../fonoteka.go/plugins/golem15/user fetch origin`, `git -C ../fonoteka.go/plugins/golem15/user log --oneline origin/master..master` prints nothing.</precondition>
<reversibility rating="costly">D-02: LoginGate, LoginGates, LoginAttempt and CheckLoginGates become exported API of the shared core plugin that sm-mfa-plugin and sm-sociallogin-plugin implement and call.</reversibility>
<files>.planning/notes/2026-10-21-mfa-otp-webauthn.md, .planning/research/STACK.md, ../fonoteka.go/plugins/golem15/user/classes/login_gate.go, ../fonoteka.go/plugins/golem15/user/classes/events.go, ../fonoteka.go/plugins/golem15/user/controllers/login_gates.go, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go, ../fonoteka.go/plugins/golem15/user/controllers/registration.go, ../fonoteka.go/plugins/golem15/user/plugin.go, ../fonoteka.go/plugins/golem15/user/login_gate_smoke_test.go, ../fonoteka.go/plugins/golem15/user/README.md</files>
<read_first>.planning/notes/2026-10-20-sociallogin-goth.md (structure to copy), .planning/research/STACK.md (Supporting Libraries table), 19.6-RESEARCH.md (Summary, Q1, Q2, Q3, Package Legitimacy Audit, Pattern 1, Code Example 1, Pitfalls 1-3 and 21, Assumptions A1 and A13), 19.6-CONTEXT.md (D-02, D-04, D-08, D-18), ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (Login :47-127), ../fonoteka.go/plugins/golem15/user/controllers/registration.go, ../fonoteka.go/plugins/golem15/user/classes/events.go, ../fonoteka.go/plugins/golem15/user/classes/oauth_providers.go, ../fonoteka.go/plugins/golem15/user/plugin.go (:56-110), ../fonoteka.go/plugins/golem15/user/session_test.go (TestLogin, sessionApp, insertUser), ../fonoteka.go/plugins/golem15/user/README.md (Features, API reference)</read_first>
<action>Planning doc first (D-04). Write `.planning/notes/2026-10-21-mfa-otp-webauthn.md` in the structure of the 2026-10-20 note: title "Decision: second factors use pquerna/otp, go-webauthn and boombuler/barcode, in sm-mfa-plugin only"; Date 2026-10-21; Status Accepted; Context Phase 19.6 D-01, D-04, D-06, D-08, D-14 and 19.6-RESEARCH.md Q1-Q3 and the Package Legitimacy Audit. Decision bullets: `github.com/pquerna/otp` v1.5.0 (published 2024-12-31, Apache-2.0, packages totp and hotp; issuer the app name, account the user's email, window plus or minus one step, SHA1, 6 digits, 30 s); `github.com/go-webauthn/webauthn` v0.18.2 (published 2026-09-19, BSD-3-Clause, packages webauthn and protocol; attestation none, timeout 60 s, user verification preferred for second-factor use and required for passkeys); `github.com/boombuler/barcode` v1.1.0 (MIT, package qr only; already required by pquerna/otp, so it adds no module) with a small SVG serializer in sm-mfa-plugin that keeps PHP's `{secret, qr_code_svg, qr_code_url}` contract (round-trip proven by an independent decoder; RESEARCH A1); the alternative piglig/go-qr v1.1.0 is rejected (67 stars, one maintainer, a new module for what boombuler already provides). Slow hash: bcrypt through `bouncer.HashPassword`/`CheckPassword` at `golem15.user.password.bcrypt_cost`, as the user plugin hashes passwords (no argon2; RESEARCH Q3, 631 ms per 10 compares). Measurements from RESEARCH Q1: a stripped demo binary grows by 2,269,184 bytes (+2.16 MiB, 51,802,375 to 54,071,559); newly linked modules boombuler/barcode v1.1.0, fxamacker/cbor/v2 v2.9.4, go-webauthn/webauthn v0.18.2, go-webauthn/x v0.3.1, google/go-tpm v0.9.8, philhofer/fwd v1.2.0, pquerna/otp v1.5.0, tinylib/msgp v1.6.4, x448/float16 v0.8.4; minimal version selection moves go-viper/mapstructure/v2 from v2.4.0 to v2.5.0 in the graphs of applications that mount the plugin; govulncheck v1.9.0 reports no reachable vulnerability (the module-level GO-2026-5932 sits in an unimported x/crypto package). go-webauthn's go.mod lists the archived YAML v3 module as an indirect, test-only requirement outside the import graph; plugin code never imports it (CLAUDE.md: goccy only). D-08 deviation recorded here: WebAuthn requests and responses use go-webauthn's standard base64url shapes, not PHP's lbuchs plain-base64 layout, because no client implements the PHP layout yet; PHP stored PEM public keys while go-webauthn verifies COSE keys, so the Phase 30 import must convert or drop WebAuthn rows. Package legitimacy: the seam cannot check Go modules, so the three are [ASSUMED] and gated by the blocking human check (plan 19.6-01 Task 5) before any `go get`. Consequences: only sm-mfa-plugin's go.mod requires them; sm-user-plugin, the framework and fonoteka.go require none; applications without the plugin link none of the nine modules (D-01); go-webauthn is pre-1.0 and the research is valid until 2026-11-20, so the executor re-checks the pin with `go list -m -json github.com/go-webauthn/webauthn@latest` before installing after that date. In .planning/research/STACK.md add three Supporting Libraries rows in the existing style (library with packages | version | purpose | "Decided in .planning/notes/2026-10-21-mfa-otp-webauthn.md (Phase 19.6 D-04); imported only by sm-mfa-plugin"). Commit the note and STACK.md together, alone: `docs(19.6): decide the OTP, WebAuthn and QR libraries`.
Then the tracer in sm-user-plugin (D-02, RESEARCH Code Example 1 and Pattern 1). classes/login_gate.go: the types and constants in this plan's Decisions with doc comments: a gate inspects a first-factor sign-in that already succeeded; a nil result allows it; a non-nil result replaces the token response with its Status (0 means 200) and Body; an error must fail the sign-in closed; another plugin adds a gate in its Boot, because every Register runs before every Boot; Fields is the already-read login body (Pitfall 2) and is nil on social sign-in. `LoginGates` guards its slice with a sync.RWMutex; `Check` on a nil receiver returns nil, nil; it clones the slice under the read lock and returns the first non-nil result or the first error, never calling later gates after either. controllers/login_gates.go: `CheckLoginGates(ctx, app, a)` looks up `*classes.LoginGates` per call and returns nil, nil when none is published. plugin.go Register publishes `classes.NewLoginGates()` with `app.Publish` (keyed on its concrete pointer type, looked up the same way).
classes/events.go adds `LoginEventName = "golem15.user.login"` and `LoginEvent{User *models.User; Source string}` (doc: fired after a sign-in issued a token: password login, a completed second factor, a passkey or a device hand-over; not on refresh). controllers/registration.go adds `FireLoginEvent(ctx, app, user, source)` modelled on FireDeactivatingEvent.
Login (controllers/api_controller.go) per D-02 and D-18 A10: keep everything up to and including `CheckAndRecordLogin(ctx, gdb, user, ip, true)` unchanged; move the bcrypt rehash block directly after it (unchanged code); then `res, err := CheckLoginGates(ctx, app, classes.LoginAttempt{User: user, Source: classes.LoginSourcePassword, Fields: fields, Request: r})`: err → `writeOpaque500(w)` and return; res non-nil → `writeJSON(w, status, res.Body)` with status res.Status or 200 when it is 0, and return (no restore, no mail, no last_seen, no token, no event); then the soft-delete restore block moved here unchanged (with its reactivate mail), TouchLastSeen, mint, apiArray, then `FireLoginEvent(ctx, app, user, classes.LoginSourcePassword)` (a listener error is logged with the user id and never fails the login), then the unchanged final writeJSON. A comment cites D-02 and D-18 A10. Refresh is not touched in this task and never consults the gate.
README (same commit as the code): Features gains "Login gates" (a plugin adds a classes.LoginGate to the published classes.LoginGates in its Boot; Login consults gates after the password and throttle checks and before restoring a deactivated account and minting; a gate answer replaces the token response; gate errors fail closed; refresh never consults gates) and "Login event"; the API reference gains rows for every identifier this task adds. Neutral wording ("a two-factor plugin").
Smoke tests in login_gate_smoke_test.go. `TestLoginGatesSmoke` (no database, runs under -short): a nil *LoginGates returns nil, nil; an empty registry returns nil, nil; gates A (nil) then B (result) return B's result; a gate returning an error stops the chain before a following gate (a counter proves the later gate was not called). `TestLoginGateSmoke` (Postgres via sessionApp, skips under -short), subtests: `no_gate` (registry published and empty: login answers 200 with token and user and a LoginEvent listener records exactly one event with Source password); `challenge` (a fake gate returns Status 200 with body two_factor_required true, challenge_token of 64 x characters, available_methods ["totp"], expires_at "2026-10-21T12:00:00+00:00": the response body decodes to exactly that map with no token key, the gate saw Source password and Fields containing trusted_device_token sent in the login body, and no LoginEvent fired); `refusal` (Status 401 with {"error":"Invalid or expired challenge."} is answered as such); `gate_error` (opaque 500, no token key); `soft_deleted_stays` (a soft-deleted user with a challenging gate: deleted_at is still set afterwards and the captured mail has no reactivate message; with the registry emptied the same login restores the account and sends one reactivate mail, as before). If sessionApp does not run Register, the test publishes `classes.NewLoginGates()` itself. Commit in sm-user-plugin: `feat: login gates and the login event`.</action>
<verify>
<automated>go -C ../fonoteka.go vet ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short -run '^TestLoginGatesSmoke$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -q -- '^--- PASS: TestLoginGatesSmoke' &amp;&amp; go -C ../fonoteka.go build ./... &amp;&amp; go -C ../sm-sociallogin-plugin build ./... &amp;&amp; go -C ../sm-oauthserver-plugin build ./...</automated>
<fails_when>Non-zero exit: vet or the short user-plugin suite is red, no "--- PASS: TestLoginGatesSmoke" line in the verbose run, or fonoteka.go, sm-sociallogin-plugin or sm-oauthserver-plugin (all three resolve sm-user-plugin to this checkout) no longer compile (a non-additive change).</fails_when>
<automated>go -C ../fonoteka.go test -count=1 -run '^(TestLoginGateSmoke|TestLogin|TestRefresh)$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 3: the gate smoke, the unchanged TestLogin or TestRefresh failed or skipped (a "--- SKIP" means Docker was unreachable: rerun through sudo -u golem, else record NOT RUN in the SUMMARY).</fails_when>
</verify>
<acceptance_criteria>
- `test -n "$(git log --diff-filter=A --format=%H -- .planning/notes/2026-10-21-mfa-otp-webauthn.md)"` exits 0, and the note contains each of `pquerna/otp v1.5.0`, `go-webauthn/webauthn v0.18.2`, `boombuler/barcode v1.1.0`, `2,269,184` and `go-viper/mapstructure` (one grep -c per literal, each at least 1).
- `grep -c '2026-10-21-mfa-otp-webauthn.md' .planning/research/STACK.md` prints at least 3.
- `grep -c 'type LoginGate interface' ../fonoteka.go/plugins/golem15/user/classes/login_gate.go` prints at least 1 and `grep -c 'CheckLoginGates(' ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go` prints at least 1.
- `grep -c 'LoginEventName = "golem15.user.login"' ../fonoteka.go/plugins/golem15/user/classes/events.go` prints 1.
- `grep -cE 'LoginGates|LoginEvent|CheckLoginGates' ../fonoteka.go/plugins/golem15/user/README.md` prints at least 3.
- `D=$(git -C ../fonoteka.go/plugins/golem15/user diff --name-only 35cf166 -- updates) && test -z "$D"` exits 0 (no migration touched by this task).
- `test "$(grep -c 'pquerna\|webauthn\|boombuler' ../fonoteka.go/plugins/golem15/user/go.mod)" -eq 0` exits 0.
</acceptance_criteria>
<done>The libraries are on record before any code, and a password login can be held back by a registered gate without changing anything for applications that register none.</done>
</task>
<task type="auto">
<name>Task 2: Password changes announce themselves inside their transaction, and an administrator with the new permission gets a "Reset 2FA" action that appears only when a two-factor plugin publishes its service</name>
<reversibility rating="costly">D-07 and D-15: PasswordChangedEvent, TwoFactorAdmin and the golem15.users.reset_two_factor permission become API and backend-role data that other plugins and imported roles reference.</reversibility>
<files>../fonoteka.go/plugins/golem15/user/classes/events.go, ../fonoteka.go/plugins/golem15/user/classes/two_factor_admin.go, ../fonoteka.go/plugins/golem15/user/controllers/registration.go, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go, ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go, ../fonoteka.go/plugins/golem15/user/controllers/users/config_form.yaml, ../fonoteka.go/plugins/golem15/user/admin_permissions.go, ../fonoteka.go/plugins/golem15/user/lang/en/lang.yaml, ../fonoteka.go/plugins/golem15/user/lang/pl/lang.yaml, ../fonoteka.go/plugins/golem15/user/password_changed_smoke_test.go, ../fonoteka.go/plugins/golem15/user/reset_two_factor_smoke_test.go, ../fonoteka.go/plugins/golem15/user/admin_registration_test.go, ../fonoteka.go/plugins/golem15/user/README.md</files>
<read_first>../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (ResetPassword :300-345, ChangePassword :560-600), ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go (:37-45, :215-320, :665-730), ../fonoteka.go/plugins/golem15/user/controllers/request_db.go, ../fonoteka.go/plugins/golem15/user/controllers/users/config_form.yaml, ../fonoteka.go/plugins/golem15/user/admin_permissions.go, ../fonoteka.go/plugins/golem15/user/classes/privileged.go, ../fonoteka.go/plugins/golem15/user/lang/en/lang.yaml (users and plugin sections), ../fonoteka.go/plugins/golem15/user/admin_harness_test.go (newAdminEnv, admin, call, expect), ../fonoteka.go/plugins/golem15/user/admin_users_test.go (TestAdminUserActions :575, TestAdminUserPassword :977), ../fonoteka.go/plugins/golem15/user/admin_registration_test.go (TestAdminRegistration :29: exact permission codes, the no-default-role rule, phrase keys in en and pl), ../fonoteka.go/plugins/golem15/user/deactivating_event_test.go, summercms.go modules/cabana/actions.go (:236-252 record-action permission and Applies handling), 19.6-RESEARCH.md (Q5 password-write sites, Q9, Pattern 5, Pitfall 3)</read_first>
<action>D-07 hook. classes/events.go adds `PasswordChangedEventName = "user.PasswordChanged"`, the three source constants and `PasswordChangedEvent{User *models.User; Tx *gorm.DB; Source string}` with the doc: fired inside the transaction that stores a new password the user or an administrator chose (reset, change, admin set), never for a rehash on login or a new account; a listener error rolls the write back; a two-factor plugin revokes trusted devices here. controllers/registration.go adds `FirePasswordChangedEvent(ctx, app, user, tx, source)` like FireDeactivatingEvent. ResetPassword and ChangePassword: wrap the existing `Updates(...)` and a `FirePasswordChangedEvent` call in one `gdb.WithContext(ctx).Transaction(func(tx *gorm.DB) error {...})`; the Updates map and every response body stay exactly as they are; a transaction error answers the opaque 500 the handler already uses for a failed write. Admin set (FormBeforeUpdate): when `changed` is true, after setting TokensValidAfter, call `FirePasswordChangedEvent(ctx, c.app(), user, requestDB(ctx, c.db), classes.PasswordChangeSourceAdmin)` (requestDB yields cabana's write transaction) and return its error so cabana rolls the save back. Never fire from the Login rehash or RegisterUser (Pitfall 3); a comment at the rehash says so.
D-15 slot (RESEARCH Q9). classes/two_factor_admin.go: `TwoFactorAdmin` interface (methods in Artifacts) with a doc that a two-factor plugin publishes it with an explicit type argument (`app.Publish[classes.TwoFactorAdmin](svc)`), the users screen looks it up per request, and Reset disables every method, deletes WebAuthn credentials and recovery codes, revokes trusted devices and fires the plugin's disabled event, all on the given transaction. classes/privileged.go style constant `PermissionResetTwoFactor = "golem15.users.reset_two_factor"` in classes. admin_permissions.go adds the permission (Tab userPermissionTab, Label `golem15.user::lang.plugin.reset_two_factor`, no Roles; doc: only a superuser or a role granted it explicitly). config_form.yaml: `recordActions: [activate, unban, unsuspend, reset_two_factor]`. AdminRecordActions appends one action built like the existing closure but with `Permissions: []string{classes.PermissionResetTwoFactor}`, `Danger: true`, labels `reset_two_factor`, `reset_two_factor_confirm`, `reset_two_factor_success`; Applies looks up `classes.TwoFactorAdmin` through `c.app()` and returns false, nil when none is published (no two-factor plugin mounted), else `HasTwoFactor(ctx, requestDB(ctx, c.db), user.ID)`; Run takes `cabana.TxFromContext(ctx)` (error when missing, as the other actions) and calls `svc.Reset(ctx, tx, user)`; a missing service in Run returns an error, never a silent success. TestAdminRegistration pins the permission catalog: its `wantCodes` gains `golem15.users.reset_two_factor` after manage_privileged_groups and its no-default-role rule covers both codes without roles (a test update for an additive code, in this commit). Lang en and pl: `plugin.reset_two_factor` ("Reset two-factor authentication of users" / Polish), `users.reset_two_factor` ("Reset 2FA"), `users.reset_two_factor_confirm` (the user signs in with the password alone until they set up 2FA again), `users.reset_two_factor_success`.
README (same commit): Features gains "Password-changed event" and "Admin: Reset 2FA" (shown only when a two-factor plugin publishes classes.TwoFactorAdmin; permission golem15.users.reset_two_factor without a default role); the Admin section lists the action; the API reference gains the new identifiers; the permissions list gains the code.
Smoke tests. password_changed_smoke_test.go `TestPasswordChangedEventSmoke` (Postgres) with subtests: `reset` (a forgot/reset flow through the handler fires one event with Source reset), `change` (change-password fires one with Source change), `admin_set` (newAdminEnv: an admin update with a new password fires one with Source admin; an update without a password fires none), `rehash_fires_none` (set bcrypt_cost one above the stored hash's cost, log in, the stored hash changes and no event fired), `listener_error_rolls_back` (a listener returning an error makes change-password answer 500 and the stored hash is unchanged). reset_two_factor_smoke_test.go `TestResetTwoFactorSlotSmoke` (admin harness, Postgres): without a published TwoFactorAdmin the user's record response offers no reset_two_factor action; with a fake service whose HasTwoFactor is true and an admin holding golem15.users.access_users and golem15.users.reset_two_factor, the action is offered with danger true and running it calls Reset once with a non-nil transaction and the user's id; an admin holding only access_users is refused by cabana's permission check. Commit in sm-user-plugin: `feat: password-changed event and the reset-2FA admin slot`.</action>
<verify>
<automated>go -C ../fonoteka.go vet ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go build ./... &amp;&amp; go -C ../sm-sociallogin-plugin build ./... &amp;&amp; go -C ../sm-oauthserver-plugin build ./...</automated>
<fails_when>Non-zero exit: vet or the short suite is red (cabana boot validation of the new record action or permission fails inside an admin test), or a dependent repository no longer compiles.</fails_when>
<automated>go -C ../fonoteka.go test -count=1 -run '^(TestPasswordChangedEventSmoke|TestResetTwoFactorSlotSmoke|TestAdminUserActions|TestAdminUserPassword|TestAdminRegistration)$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 5</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 5: a new smoke, an existing admin action or password test, or the permission-catalog test failed or skipped (Docker unreachable: rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'type PasswordChangedEvent struct' ../fonoteka.go/plugins/golem15/user/classes/events.go` prints 1 and `grep -c 'FirePasswordChangedEvent(' ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go` prints at least 2 and `grep -c 'FirePasswordChangedEvent(' ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go` prints at least 1.
- `grep -c 'reset_two_factor' ../fonoteka.go/plugins/golem15/user/controllers/users/config_form.yaml` prints 1 and `grep -c 'golem15.users.reset_two_factor' ../fonoteka.go/plugins/golem15/user/admin_permissions.go ../fonoteka.go/plugins/golem15/user/classes/*.go | awk -F: '{s+=$2} END {print s}'` prints at least 1.
- `grep -c 'Lookup\[classes.TwoFactorAdmin\]' ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go` prints at least 1.
- `grep -c 'reset_two_factor' ../fonoteka.go/plugins/golem15/user/lang/en/lang.yaml` prints at least 3 and the same count for lang/pl/lang.yaml is at least 3.
- `grep -cE 'PasswordChangedEvent|TwoFactorAdmin|reset_two_factor' ../fonoteka.go/plugins/golem15/user/README.md` prints at least 3.
</acceptance_criteria>
<done>Every user or administrator password change is announced inside its own write, and the Users screen can offer a two-factor reset that only appears where a two-factor plugin is mounted.</done>
</task>
<task type="auto">
<name>Task 3: A new device signs in by QR or short code: initiate shows a token, short code and auth_url, a signed-in user authorizes it, and the device's first poll receives a one-time JWT exactly once</name>
<reversibility rating="costly">D-11, D-16: the golem15_user_device_auth_sessions table and the five device routes become part of the core plugin's published schema and contract once plan 03 releases them; a later change needs a migration.</reversibility>
<files>../fonoteka.go/plugins/golem15/user/updates/202610210002_create_device_auth_sessions.go, ../fonoteka.go/plugins/golem15/user/models/device_auth_session.go, ../fonoteka.go/plugins/golem15/user/classes/device_auth.go, ../fonoteka.go/plugins/golem15/user/controllers/devices.go, ../fonoteka.go/plugins/golem15/user/routes.go, ../fonoteka.go/plugins/golem15/user/config/config.yaml, ../fonoteka.go/plugins/golem15/user/device_flow_smoke_test.go, ../fonoteka.go/plugins/golem15/user/README.md</files>
<read_first>../fonoteka.go/plugins/golem15/user/updates/202610040001_create_api_tokens.go, ../fonoteka.go/plugins/golem15/user/updates/registry.go, ../fonoteka.go/plugins/golem15/user/models/api_token.go, ../fonoteka.go/plugins/golem15/user/models/registry.go, ../fonoteka.go/plugins/golem15/user/classes/api_tokens.go (:48-111), ../fonoteka.go/plugins/golem15/user/controllers/api_tokens.go, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (authenticate :795-825, mintFor :877-884, requestURL, writeJSON), ../fonoteka.go/plugins/golem15/user/routes.go, ../fonoteka.go/plugins/golem15/user/config/config.yaml, ../fonoteka.go/plugins/golem15/user/updates/postgres_test.go (migration test harness), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/ApiController.php (:1233-1347), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/models/DeviceAuthSession.php, /media/nvme/dev/golem15/queststream.online/vue-queststream-app/composables/useDeviceAuth.ts, 19.6-RESEARCH.md (Q4 device flow and tables, Pattern 2, Pattern 4, Code Example 7, Pitfalls 13, 14, 16, 17)</read_first>
<action>Per D-03, D-06, D-11, D-12a, D-12c, D-16; additive only.
Migration updates/202610210002_create_device_auth_sessions.go (raw SQL like the api-tokens migration, registered in init): `CREATE TABLE golem15_user_device_auth_sessions` with id BIGSERIAL PRIMARY KEY; token VARCHAR(64) NOT NULL UNIQUE (the SHA-256 hex of the 64-character token, D-06; PHP's column name, D-16); short_code VARCHAR(9) NULL UNIQUE; user_id INTEGER NULL REFERENCES users(id) ON DELETE CASCADE; status VARCHAR(16) NOT NULL DEFAULT 'pending' with a CHECK on the five PHP values; expires_at TIMESTAMPTZ NULL; device_ip VARCHAR(255) NULL; device_user_agent TEXT NULL; device_name VARCHAR(255) NULL; auth_ip VARCHAR(255) NULL; confirmed_at, used_at, last_activity_at TIMESTAMPTZ NULL; session_id VARCHAR(255) NULL (PHP column kept for the import); jwt_jti VARCHAR(64) NULL (D-12b); created_at and updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(); indexes on user_id, status, expires_at and jwt_jti. Rollback drops the table. models/device_auth_session.go: `DeviceAuthSession` with explicit column tags (TokenHash `gorm:"column:token"` and `json:"-"`), TableName, Fillable, Hidden as models/api_token.go, the five status constants, registered in init. If a user-plugin test counts models or migrations, update its expected count in the same commit with a comment naming this table.
classes/device_auth.go: `DeviceShortCodeAlphabet = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"`; `NewDeviceToken()` = 48 bytes from crypto/rand as base64.RawURLEncoding (64 characters); `NewDeviceShortCode()` = 8 characters drawn uniformly from the alphabet with crypto/rand (rejection sampling, no modulo bias), formatted XXXX-XXXX; `NormalizeDeviceShortCode(s)` uppercases, removes spaces and hyphens, requires exactly 8 alphabet characters and returns the XXXX-XXXX form (false otherwise); `DeviceAuthTTL(app)` from `golem15.user.device_auth.ttl_minutes` (default 5); `DeviceAuthURL(app, token)` replaces `{frontend_url}` (golem15.user.frontend_url trimmed of a trailing slash, else app.url) and `{token}` in `golem15.user.device_auth.auth_url` (default `{frontend_url}/device/scan/{token}`); `DeviceDisplayName(stored, userAgent)` ports PHP's accessor (:337-361). Hash tokens with the existing `HashAPIToken`. config/config.yaml documents the three new keys with comments (frontend_url empty means app.url; the template placeholders; the TTL).
controllers/devices.go (handler factories taking *backpack.App, JSON through the plugin's writeJSON, timestamps through wire.Time so `+00:00` matches PHP's toIso8601String):
- `DeviceInitiate` (unauthenticated): reads `device_name` from JSON or form, trims it and rejects more than 255 characters with 422 through writeValidation (Pitfall 14); first deletes at most 500 rows with status pending or expired whose expires_at is older than one hour (Pitfall 16; one bounded DELETE ... WHERE id IN (SELECT ... LIMIT 500)); generates token and short code, retrying the insert up to 5 times on a short_code unique violation; stores the token hash, status pending, expires_at now + TTL, device_ip `RemoteIP(r)`, device_user_agent; answers 200 `{"success":true,"data":{"token","short_code","auth_url","expires_at"}}`.
- `DeviceStatus` (unauthenticated, GET devices/status/{token}): look up by HashAPIToken of the path value; unknown → 404 `{"error":"Session not found"}`; status pending or confirmed with expires_at in the past → one guarded `UPDATE ... SET status='expired' WHERE id=? AND status IN ('pending','confirmed')` and the flat body `{"success":true,"status":"expired","message":"Authorization session has expired"}`; status used, revoked or expired → `{"success":true,"data":{"status":<status>}}` (used never becomes expired, Pitfall 13); pending → `{"success":true,"data":{"status":"pending"}}`; confirmed with a user → the D-12a hand-over (RESEARCH Code Example 7): load the user (not soft-deleted; if missing, mark the row revoked and answer status revoked), mint with `bouncer.Mint(secret, strconv.FormatUint(uint64(user.ID), 10), requestURL(r), ttl)` using the same ttl rule as mintFor (an unexported helper `mintWithJTI` beside mintFor), then `UPDATE golem15_user_device_auth_sessions SET status='used', used_at=NOW(), last_activity_at=NOW(), jwt_jti=?, updated_at=NOW() WHERE id=? AND status='confirmed' AND expires_at > NOW()`; RowsAffected 1 → `FinishLogin(ctx, app, gdb, user, RemoteIP(r))`, `FireLoginEvent(..., classes.LoginSourceDevice)` (logged on error) and 200 `{"success":true,"data":{"status":"confirmed","user_id":<id>,"message":"Device authorized - complete login","token":<jwt>,"user":<APIArray>}}`; RowsAffected 0 → the minted token is discarded unsent and the handler re-reads the row and answers its current status as above.
- `DeviceAuthorize` (POST, bearer JWT checked inside the handler like `authenticate` but answering PHP's 401 `{"error":"Unauthorized"}` for a missing, invalid, expired, blacklisted or stale token; D-12c, no second-factor check): with a `short_code` field present, strip hyphens and require length 8 else 422 `{"error":"Please enter a valid 8-character code"}`, then match `NormalizeDeviceShortCode`; else with a `token` field match its hash; then one guarded `UPDATE ... SET status='confirmed', user_id=?, confirmed_at=NOW(), auth_ip=?, updated_at=NOW() WHERE id=? AND status='pending' AND (user_id IS NULL OR user_id=?) AND expires_at > NOW()`; RowsAffected 1, or the row already confirmed for this caller → 200 `{"success":true,"message":"Device authorized successfully"}`; nothing matched or not updatable → 400 `{"error":"Invalid or expired code"}`.
routes.go: inside the existing user-api group add `g.Post("/devices/initiate", ...)`, `g.Get("/devices/status/{token}", ...)`, `g.Post("/devices/authorize", ...)` (no route middleware; authentication is in the handler).
README (same commit): a "Device authorization" section (QR and short-code flow, the five statuses, the one-time token on the first poll after confirmation, the configurable auth_url, tokens stored hashed, PHP's response bodies), Configuration rows for the three keys, API reference rows. Smoke tests in device_flow_smoke_test.go: `TestDeviceCodesSmoke` (no database, -short: 1000 tokens are 64 base64url characters and distinct; short codes match `^[2-9A-HJKMNP-Z]{4}-[2-9A-HJKMNP-Z]{4}$`; normalize accepts lower case, spaces and a missing hyphen and refuses 7 or 9 characters and excluded letters such as O, I, L, 0 and 1; DeviceAuthURL with and without frontend_url and with a custom template). `TestDeviceFlowSmoke` (Postgres): initiate answers the data block with a 64-character token, a short code, an auth_url containing the token and an expires_at in `+00:00` form, and the stored token column equals HashAPIToken(token); status is pending; authorize with the lower-cased short code and a valid bearer answers success; the next status answers confirmed with a token that `bouncer.VerifyClaims` accepts for the authorizing user and stores its jti; the following status answers used without a token; 10 concurrent polls on another confirmed session yield exactly one token; authorize without a bearer answers 401 `{"error":"Unauthorized"}`; a session past expires_at answers the flat expired body. Commit in sm-user-plugin: `feat: device authorization by QR or short code`.</action>
<verify>
<automated>go -C ../fonoteka.go vet ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short -run '^TestDeviceCodesSmoke$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -q -- '^--- PASS: TestDeviceCodesSmoke' &amp;&amp; go -C ../fonoteka.go build ./... &amp;&amp; go -C ../sm-sociallogin-plugin build ./... &amp;&amp; go -C ../sm-oauthserver-plugin build ./...</automated>
<fails_when>Non-zero exit: vet or the short suite is red, no "--- PASS: TestDeviceCodesSmoke" line, or a dependent repository no longer compiles.</fails_when>
<automated>go -C ../fonoteka.go test -count=1 -run '^(TestDeviceFlowSmoke|TestLoginGateSmoke)$' -v ./plugins/golem15/user/... 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 2: the device flow or the gate smoke failed or skipped (Docker unreachable: rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'golem15_user_device_auth_sessions' ../fonoteka.go/plugins/golem15/user/updates/202610210002_create_device_auth_sessions.go` prints at least 2 and `grep -c 'jwt_jti' ../fonoteka.go/plugins/golem15/user/updates/202610210002_create_device_auth_sessions.go` prints at least 1.
- `S=$(git -C ../fonoteka.go/plugins/golem15/user diff --name-status 35cf166 -- updates) && test "$(printf '%s\n' "$S" | grep -c '^[MDR]')" -eq 0` exits 0 (under updates/ only files were added).
- `grep -c "status = 'confirmed'" ../fonoteka.go/plugins/golem15/user/controllers/devices.go` prints at least 1 and `grep -c "status = 'pending'" ../fonoteka.go/plugins/golem15/user/controllers/devices.go` prints at least 1 (guarded transitions).
- `grep -c '"/devices/status/{token}"' ../fonoteka.go/plugins/golem15/user/routes.go` prints 1 and `grep -c '"/devices/initiate"' ../fonoteka.go/plugins/golem15/user/routes.go` prints 1.
- `grep -c 'device_auth' ../fonoteka.go/plugins/golem15/user/config/config.yaml` prints at least 1 and `grep -c 'frontend_url' ../fonoteka.go/plugins/golem15/user/config/config.yaml` prints at least 1.
- `grep -c 'Device authorization' ../fonoteka.go/plugins/golem15/user/README.md` prints at least 1.
</acceptance_criteria>
<done>A television or desktop app can show a QR or short code, a signed-in user approves it from another device, and the waiting device receives one token exactly once.</done>
</task>
<task type="auto">
<name>Task 4: A user lists the devices they signed in and revokes one, and the revoked device is signed out at once, also after it refreshed its token</name>
<files>../fonoteka.go/plugins/golem15/user/controllers/devices.go, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go, ../fonoteka.go/plugins/golem15/user/classes/device_auth.go, ../fonoteka.go/plugins/golem15/user/routes.go, ../fonoteka.go/plugins/golem15/user/device_revoke_smoke_test.go, ../fonoteka.go/plugins/golem15/user/README.md</files>
<read_first>../fonoteka.go/plugins/golem15/user/controllers/devices.go and classes/device_auth.go (Task 3), ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (Logout blacklist call, Refresh :179-213, refreshTTL, blacklistGrace), ../fonoteka.go/plugins/golem15/user/controllers/api_tokens.go (List and Destroy), summercms.go modules/bouncer/refresh.go (:92-155), modules/bouncer/blacklist.go, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/ApiController.php (:1175-1227), 19.6-RESEARCH.md (Q7, Pattern 4, Code Example 8, Pitfalls 15, 17)</read_first>
<action>Per D-12b and D-11. `DeviceIndex` (GET devices, bearer checked in the handler with PHP's 401 body): rows of the caller with status confirmed or used ordered by last_activity_at DESC NULLS LAST, then id DESC; each item `{id, device_name: DeviceDisplayName(row.DeviceName, row.DeviceUserAgent), device_ip, authorized_at: confirmed_at as wire.Time or null, last_activity: last_activity_at as wire.Time or null, is_current: row.jwt_jti equals the bearer token's jti}`; answer `{"success":true,"devices":[...]}` with wire.Slice so an empty list is `[]`. `DeviceDestroy` (DELETE devices/{id}): parse the id, find the row scoped by `user_id` of the caller (else 404 `{"error":"Device not found"}`); in one transaction set status revoked and updated_at with a guarded UPDATE scoped by id and user_id, and when the row has a jwt_jti add it to the published `bouncer.BlacklistStore` with expiresAt = now + refreshTTL + the JWT ttl + 1 minute and validUntil = now (so the token and every refresh of it are refused at once; RESEARCH Q7); a blacklist error answers the opaque 500 and rolls the status back; success answers `{"success":true,"message":"Device revoked successfully"}`. Revoking an already revoked row answers the same success (PHP's revoke() saved unconditionally). routes.go: `g.Get("/devices", ...)` and `g.Delete("/devices/{id}", ...)` immediately followed by `g.Where("id", "[0-9]+")` (surf binds Where to the last route, Pitfall 17).
Refresh JTI hand-over (RESEARCH Q7, Code Example 8). classes/device_auth.go `HandOverDeviceJTI(ctx, db, oldJTI, newJTI)` runs `UPDATE golem15_user_device_auth_sessions SET jwt_jti = ?, last_activity_at = NOW(), updated_at = NOW() WHERE jwt_jti = ? AND status = 'used'` and does nothing for empty values. In `Refresh`, after `bouncer.Refresh` succeeded: read the old token's jti from the raw token's payload segment with the standard library (split on dots, base64.RawURLEncoding decode of the middle part, JSON field jti; safe because bouncer.Refresh has just verified the signature), read the new jti from `bouncer.VerifyClaims(next, secret)` (the block that already parses it for TouchLastSeen), and call HandOverDeviceJTI; an error is logged with no token or jti value and never fails the refresh; the response body stays `{"token": next}`. A comment documents Pitfall 15: with golem15.user.jwt.blacklist_grace above 0 two concurrent refreshes of one token can fork a device's lineage and only one fork is tracked; a password change still ends both through tokens_valid_after.
README (same commit): the Device authorization section gains the list and revoke routes, is_current, the immediate sign-out on revoke including refreshed tokens, and the blacklist_grace caveat; API reference row for HandOverDeviceJTI. Smoke test device_revoke_smoke_test.go `TestDeviceRevokeSmoke` (Postgres) with subtests: `list` (two used sessions and one pending of the caller plus one of another user: the list has the caller's two, newest activity first, device_name from the stored name or the parsed user agent, is_current true only for the session whose token is the bearer); `revoke` (DELETE answers success; GET /fetch with the device token answers 401; DELETE of another user's id answers 404; DELETE /devices/abc does not match the route); `revoke_after_refresh` (the device refreshes its token, the row's jwt_jti now equals the new jti, revoke, then both the old and the refreshed token answer 401 on /fetch and refreshing the refreshed token fails). Commit in sm-user-plugin: `feat: device list and revoke with the jti followed through refresh`.</action>
<verify>
<automated>go -C ../fonoteka.go vet ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go test -count=1 -short ./plugins/golem15/user/... &amp;&amp; go -C ../fonoteka.go build ./... &amp;&amp; go -C ../sm-sociallogin-plugin build ./... &amp;&amp; go -C ../sm-oauthserver-plugin build ./...</automated>
<fails_when>Non-zero exit: vet or the short suite is red, or a dependent repository no longer compiles.</fails_when>
<automated>go -C ../fonoteka.go test -count=1 -run '^(TestDeviceRevokeSmoke|TestDeviceFlowSmoke|TestRefresh|TestLogout)$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 4</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 4: revoke, the device flow, or the unchanged refresh or logout tests failed or skipped (Docker unreachable: rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'HandOverDeviceJTI(' ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go` prints at least 1.
- `grep -c 'Lookup\[bouncer.BlacklistStore\]' ../fonoteka.go/plugins/golem15/user/controllers/devices.go` prints at least 1.
- `grep -A1 '"/devices/{id}"' ../fonoteka.go/plugins/golem15/user/routes.go | grep -c 'Where("id", "\[0-9\]+")'` prints 1 (the constraint follows the route directly).
- `grep -c '"Device revoked successfully"' ../fonoteka.go/plugins/golem15/user/controllers/devices.go` prints 1 and `grep -c '"Device not found"' ../fonoteka.go/plugins/golem15/user/controllers/devices.go` prints 1.
- `grep -c 'blacklist_grace' ../fonoteka.go/plugins/golem15/user/README.md` prints at least 1.
- `test -z "$(git -C ../fonoteka.go/plugins/golem15/user status --porcelain)"` exits 0 after the commit, and `C=$(git -C ../fonoteka.go/plugins/golem15/user rev-list --count 35cf166..HEAD) && test "$C" -ge 4` exits 0.
</acceptance_criteria>
<done>A user sees which devices they signed in, and revoking one ends its session immediately, even if it refreshed its token in the meantime.</done>
</task>
<task type="checkpoint:human-verify" gate="blocking-human">
<name>Task 5: Package legitimacy check before installing github.com/pquerna/otp v1.5.0, github.com/go-webauthn/webauthn v0.18.2 and github.com/boombuler/barcode v1.1.0 (runtime dependencies of sm-mfa-plugin only)</name>
<read_first>.planning/notes/2026-10-21-mfa-otp-webauthn.md, 19.6-RESEARCH.md (Q1, Q2, Package Legitimacy Audit, Assumption A13)</read_first>
<acceptance_criteria>
- The SUMMARY records the human's reply verbatim ("approved" or "rejected") under a "Task 5 reply" heading.
- Until the reply, `test ! -e ../sm-mfa-plugin/go.mod` exits 0 (plan 02 has not started), and `grep -c 'go-webauthn' go.mod ../fonoteka.go/plugins/golem15/user/go.mod ../fonoteka.go/go.mod | awk -F: '{s+=$2} END {print s}'` prints 0.
</acceptance_criteria>
<what-built>Nothing installed. Tasks 1-4 are committed locally: the decision note naming the three libraries, and sm-user-plugin's login gates, login and password-changed events, reset-2FA slot and device authorization flow. RESEARCH verified the three modules on proxy.golang.org (pquerna/otp v1.5.0 published 2024-12-31, Apache-2.0, 2,963 stars; go-webauthn/webauthn v0.18.2 published 2026-09-19, BSD-3-Clause, 1,351 stars, pushed 2026-10-20; boombuler/barcode v1.1.0 published 2025-07-22, MIT, 1,563 stars and already a requirement of pquerna/otp), measured the +2.16 MiB binary delta and the nine newly linked modules, and ran govulncheck (clean). The package-legitimacy seam cannot check Go modules, so all three carry [ASSUMED] and need your check (19.4 and 19.5 precedent). They will be required only by sm-mfa-plugin and appear in the module graph of applications that mount it (sm-demo-app, sm-bm-app). Never auto-approved.</what-built>
<how-to-verify>
1. Open https://pkg.go.dev/github.com/pquerna/otp, https://pkg.go.dev/github.com/go-webauthn/webauthn and https://pkg.go.dev/github.com/boombuler/barcode and their GitHub repositories: confirm the owners, the licences and that v1.5.0, v0.18.2 and v1.1.0 are tagged releases.
2. In a scratch directory run `go list -m -json github.com/go-webauthn/webauthn@v0.18.2` (and the same for the other two) and confirm Origin.URL points at the GitHub repository and Origin.Ref at the tag.
3. Confirm the three are acceptable as runtime dependencies of sm-mfa-plugin only (never in the framework, sm-user-plugin or fonoteka.go).
</how-to-verify>
<resume-signal>Reply "approved" to let plan 02 run `go get` for the three pins in sm-mfa-plugin, or "rejected" to stop: plan 02 must then not start, because D-04 locks pquerna/otp and go-webauthn, and the phase returns to /msd-discuss-phase 19.6.</resume-signal>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| first-factor login to token mint | a correct password must not yield a token while a second factor is pending |
| sm-user-plugin to other plugins | gates, the admin reset service and event listeners run code from sm-mfa-plugin inside the user plugin's requests and transactions |
| unauthenticated device to devices/initiate and devices/status | anyone can create sessions and poll with a token |
| signed-in user to devices/authorize, list and revoke | a bearer JWT binds or ends device sessions |
| JWT refresh to device revocation | a refreshed device token must stay revocable |
| administrator to the Users screen | a privileged action removes a user's second factors |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6-01 | Elevation of privilege | Login gate consultation (controllers/api_controller.go, CheckLoginGates) | high | mitigate | gate errors answer the opaque 500 and never fall through to the mint; a non-nil result returns before restore, last_seen, mint and event; TestLoginGateSmoke subtests challenge, refusal and gate_error |
| T-19.6-02 | Tampering | soft-delete restore before the second factor (Pitfall 21, D-18 A10) | medium | mitigate | the restore and reactivate mail run after the gate; TestLoginGateSmoke soft_deleted_stays |
| T-19.6-03 | Spoofing | trusted devices surviving a password change (D-07) | medium | mitigate | PasswordChangedEvent fires inside the password-write transaction from reset, change and admin set, a listener error rolls the write back, the rehash fires none; TestPasswordChangedEventSmoke |
| T-19.6-04 | Elevation of privilege | reset_two_factor record action (D-15) | medium | mitigate | new permission without a default role, Danger confirm, cabana's permission check and audit log, Applies false without a published service, Run errors without one; TestResetTwoFactorSlotSmoke |
| T-19.6-05 | Tampering | short-code guessing to bind a victim's pending device | medium | mitigate | 31^8 code space from crypto/rand without modulo bias, 5-minute TTL, user-api bucket, guarded pending-to-confirmed UPDATE that requires user_id NULL or the caller |
| T-19.6-06 | Spoofing | device-code phishing (attacker shows its QR to a victim who approves it) | medium | accept | D-12c keeps PHP's authorize-with-JWT rule; the device list shows name and IP and revoke signs the device out at once; the frontend is expected to show device name and IP before confirming |
| T-19.6-07 | Elevation of privilege | device hand-over race (two polls, two tokens) | high | mitigate | single guarded confirmed-to-used UPDATE; a poll that loses never sends its minted token; TestDeviceFlowSmoke 10 concurrent polls yield one token |
| T-19.6-08 | Elevation of privilege | revoked device keeps refreshing (RESEARCH Q7) | high | mitigate | Refresh hands the row's jwt_jti to the new token; revoke blacklists the current jti for the refresh window; TestDeviceRevokeSmoke revoke_after_refresh; blacklist_grace fork documented (Pitfall 15) |
| T-19.6-09 | Information disclosure | device tokens at rest and in URLs | medium | mitigate | only the SHA-256 hex is stored (D-06); the framework access log records no paths; reverse-proxy logging of devices/status/{token} is documented in the README; tokens live 5 minutes and are one-time |
| T-19.6-10 | Denial of service | unauthenticated devices/initiate filling the table (Pitfall 16) | low | mitigate | user-api bucket (120/min) and a bounded cleanup of stale pending and expired rows on each initiate |
| T-19.6-11 | Tampering | attacker-chosen device_name (Pitfall 14) | low | mitigate | trimmed, max 255 with a 422, rendered by clients as text; the parsed user agent and IP shown beside it |
| T-19.6-12 | Tampering | core plugin contract (19.4 D-05, CLAUDE.md) | medium | mitigate | only added exports, routes, config keys and one added migration; TestLogin, TestRefresh, TestLogout and the admin tests pass unchanged; fonoteka.go, sm-sociallogin-plugin and sm-oauthserver-plugin compile in every verify |
| T-19.6-SC | Tampering | Go module installs | high | mitigate | no module is installed in this plan; the three pins wait for the Task 5 blocking human check (never auto-approved); acceptance criteria grep go.mod files for go-webauthn |
</threat_model>
<verification>
- Task 1: the note precedes the code; TestLoginGatesSmoke (short), TestLoginGateSmoke, TestLogin and TestRefresh; dependent repositories compile.
- Task 2: TestPasswordChangedEventSmoke, TestResetTwoFactorSlotSmoke, TestAdminUserActions, TestAdminUserPassword.
- Task 3: TestDeviceCodesSmoke (short), TestDeviceFlowSmoke; only added files under updates/.
- Task 4: TestDeviceRevokeSmoke, TestDeviceFlowSmoke, TestRefresh, TestLogout.
- Task 5: the human reply recorded; no go.mod requires the libraries before it.
- Feedback sampling (19.6-VALIDATION.md): after each commit `go -C ../fonoteka.go vet ./plugins/golem15/user/... && go -C ../fonoteka.go test -short ./plugins/golem15/user/...`.
</verification>
<success_criteria>
- D-04: decision note on record before any library code; libraries gated by a human check.
- D-02 and D-18 A10: Login consults gates after the password step and before restore and mint; unchanged without gates.
- D-07 and D-15: password-changed event from three sites; reset slot offered only with a published service.
- D-03, D-11, D-12a, D-12b, D-12c, D-16: device flow with PHP's paths and bodies, one-time JWT, revoke across refresh.
- Additive: every v0.1.5 test passes, dependent repositories compile, nothing pushed.
</success_criteria>
<output>
Create `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-01-SUMMARY.md` when done, with the commits in order and their repositories (planning docs first), the base commit of sm-user-plugin the phase started from (plan 04's additive check uses it), the final exported identifiers and config keys (plans 02 and 03 call them), the Task 5 reply verbatim, which Docker-backed tests ran or were NOT RUN, and any deviation from this plan's names. Post a short comment on the Phase 19.6 milestone task in g15office (`avoroeb4`, .planning/G15OFFICE.md) naming plan 19.6-01's results; if the g15office tool is not available in this session, record that in the SUMMARY for the orchestrator.
</output>

View File

@@ -0,0 +1,356 @@
---
phase: 19.6-two-factor-auth-and-device-flow-in-sm-user-plugin
plan: 02
type: execute
wave: 2
depends_on: ["19.6-01"]
files_modified:
- ../sm-mfa-plugin/.gitignore
- ../sm-mfa-plugin/go.mod
- ../sm-mfa-plugin/go.sum
- ../sm-mfa-plugin/go.work
- ../sm-mfa-plugin/plugin.go
- ../sm-mfa-plugin/routes.go
- ../sm-mfa-plugin/buckets.go
- ../sm-mfa-plugin/mail.go
- ../sm-mfa-plugin/admin_settings.go
- ../sm-mfa-plugin/admin_permissions.go
- ../sm-mfa-plugin/config/config.yaml
- ../sm-mfa-plugin/lang/en/lang.yaml
- ../sm-mfa-plugin/lang/pl/lang.yaml
- ../sm-mfa-plugin/views/mail/two_factor_code.htm
- ../sm-mfa-plugin/views/mail/two_factor_code-en.htm
- ../sm-mfa-plugin/classes/config.go
- ../sm-mfa-plugin/classes/gate.go
- ../sm-mfa-plugin/classes/challenge.go
- ../sm-mfa-plugin/classes/login.go
- ../sm-mfa-plugin/classes/totp.go
- ../sm-mfa-plugin/classes/qr.go
- ../sm-mfa-plugin/classes/recovery.go
- ../sm-mfa-plugin/classes/email.go
- ../sm-mfa-plugin/classes/trusted.go
- ../sm-mfa-plugin/classes/methods.go
- ../sm-mfa-plugin/classes/events.go
- ../sm-mfa-plugin/classes/admin.go
- ../sm-mfa-plugin/controllers/helpers.go
- ../sm-mfa-plugin/controllers/challenge.go
- ../sm-mfa-plugin/controllers/management.go
- ../sm-mfa-plugin/models/registry.go
- ../sm-mfa-plugin/models/two_factor_method.go
- ../sm-mfa-plugin/models/two_factor_challenge.go
- ../sm-mfa-plugin/models/two_factor_recovery_code.go
- ../sm-mfa-plugin/models/trusted_device.go
- ../sm-mfa-plugin/models/settings.go
- ../sm-mfa-plugin/models/string_list.go
- ../sm-mfa-plugin/models/settings/fields.yaml
- ../sm-mfa-plugin/updates/registry.go
- ../sm-mfa-plugin/updates/202610220001_create_golem15_user_two_factor_methods.go
- ../sm-mfa-plugin/updates/202610220002_create_golem15_user_two_factor_challenges.go
- ../sm-mfa-plugin/updates/202610220003_create_golem15_user_two_factor_recovery_codes.go
- ../sm-mfa-plugin/updates/202610220004_create_golem15_user_trusted_devices.go
- ../sm-mfa-plugin/updates/202610220005_create_golem15_mfa_settings.go
- ../sm-mfa-plugin/testmain_test.go
- ../sm-mfa-plugin/harness_test.go
- ../sm-mfa-plugin/tracer_smoke_test.go
- ../sm-mfa-plugin/challenge_smoke_test.go
- ../sm-mfa-plugin/trusted_settings_smoke_test.go
- ../sm-mfa-plugin/README.md
autonomous: true
requirements: [SC-1, SC-2, SC-4, D-01, D-04, D-05, D-06, D-07, D-09, D-10, D-12, D-13, D-15, D-16, D-18, D-19]
estimate:
tokens: 340000
raw_tokens: 340000
tasks: 3
confidence: low
must_haves:
truths:
- "D-01: a new local repository sm-mfa-plugin (module git.golem15.com/golem15/sm-mfa-plugin, package mfa, plugin ID golem15.mfa) requires golem15.user; only its go.mod requires pquerna/otp v1.5.0 and boombuler/barcode v1.1.0 (go-webauthn arrives in plan 03); its Boot needs no database, cache or mailer."
- "SC-2, D-02, D-05: with the plugin mounted and TOTP enabled, POST /_user/api/v1/login answers 200 {two_factor_required:true, challenge_token (64 characters), available_methods (the user's enabled methods, unfiltered by the global list), expires_at (+00:00)} and no token; the challenge routes /_user/api/v1/2fa/{verify,recovery,email/send} are unauthenticated and share the 2fa-verify bucket of 10 requests per minute per IP (the 11th answers 429)."
- "D-05: POST 2fa/verify, 2fa/recovery and 2fa/email/send answer PHP's validation 422 bodies, PHP's 401 bodies ({\"error\":\"Invalid verification code.\",\"two_factor_failed\":true}, {\"error\":\"Invalid recovery code.\",\"two_factor_failed\":true}, {\"error\":\"Invalid or expired challenge.\"}) and on success {token, user} from the user plugin's helpers, followed by the login event and golem15.user.2fa.verified; a challenge lives two_factor_challenge_ttl minutes (5), allows five verifications (each verify increments attempts in one guarded UPDATE, recorded deviation A5) and completes at most once, so parallel verifies mint at most one token."
- "D-04, D-05: POST 2fa/totp/setup answers {secret, qr_code_svg, qr_code_url} (otpauth URL with the app name as issuer and the email as account; SVG 200x200 with a viewBox, drawn from boombuler/barcode's matrix); totp/confirm verifies with a one-step window and stores the secret encrypted (lagoon.Encrypted); a TOTP code is accepted once per time step (last counter in metadata, Pitfall 5); DELETE totp disables it."
- "D-05, D-19: recovery codes come in sets of 10 in the form XXXXX-XXXXX over 23456789ABCDEFGHJKMNPQRSTUVWXYZ, stored as bcrypt hashes and consumed once through a guarded UPDATE; GET recovery-codes?password= answers only the remaining count; POST recovery-codes/regenerate replaces the set in one transaction and returns the new codes; when totp/confirm or email/enable creates the first set (no unused code existed), the response adds recovery_codes, otherwise it is PHP's body unchanged."
- "D-05, D-18 (A8, A9): the email code is 6 digits stored as a bcrypt hash and mailed through golem15.mfa::mail.two_factor_code (-en for English) with name, code and expiry_minutes = min(two_factor_email_code_ttl, remaining challenge minutes); email/send and verify method=email work only when the user's email method is enabled (else 401 {\"error\":\"Invalid or expired challenge.\"}) and at most 3 sends per challenge."
- "D-18 (A6): after 10 failed second-factor verifications of one user within 15 minutes, counted from challenge rows in the database, new challenges are refused at login with 401 {\"error\":\"Invalid or expired challenge.\"} and every verify endpoint answers its generic 401, until the window passes; failed challenge rows are kept for the window."
- "D-06: challenge and trusted-device tokens are stored as SHA-256 hex in PHP's token columns, the TOTP secret as lagoon.Encrypted ciphertext, recovery and email codes as bcrypt hashes; no token, code or secret is logged."
- "D-07, D-10: with trusted_device_enabled on, a verify with trust_device adds trusted_device_token, a login sending it skips the challenge until trusted_device_ttl_days (30) pass; any PasswordChangedEvent and disabling the user's last method delete the user's trusted devices (the latter also the recovery codes, as PHP); DELETE trusted-devices/{id} and DELETE trusted-devices revoke them."
- "D-12, D-13, D-09 (setting): an admin Settings screen (code mfa, permission golem15.mfa.access_settings) holds two_factor_mode (disabled|optional|enforced, default optional), two_factor_available_methods (default [totp,email], allow-list totp,email,webauthn), two_factor_enforce_groups (existing group codes), two_factor_email_code_ttl 10, two_factor_challenge_ttl 5, two_factor_passwordless_login off, trusted_device_enabled off, trusted_device_ttl_days 30 and two_factor_social_login off; config supplies each default; a saved row applies on the next request; only mode disabled exempts a user (no child exemption)."
- "D-15: the plugin publishes userclasses.TwoFactorAdmin; the user plugin's reset_two_factor action then disables every method, deletes recovery codes and trusted devices and fires golem15.user.2fa.disabled inside cabana's transaction."
- "D-16: migrations create golem15_user_two_factor_methods, golem15_user_two_factor_challenges, golem15_user_two_factor_recovery_codes and golem15_user_trusted_devices with PHP's names and columns (plus the challenge email_sends counter) and golem15_mfa_settings with its seeded row 1."
artifacts:
- path: "../sm-mfa-plugin/plugin.go"
provides: "golem15.mfa plugin, gate registration, published TwoFactorAdmin, listeners"
contains: "golem15.mfa"
- path: "../sm-mfa-plugin/classes/challenge.go"
provides: "challenge creation and guarded attempt and completion UPDATEs"
contains: "attempts < max_attempts"
- path: "../sm-mfa-plugin/classes/qr.go"
provides: "QRSVG over boombuler/barcode"
contains: "viewBox"
- path: "../sm-mfa-plugin/buckets.go"
provides: "2fa-verify bucket"
contains: "2fa-verify"
- path: "../sm-mfa-plugin/tracer_smoke_test.go"
provides: "TestTracerSmoke and TestQRSVGSmoke"
contains: "TestTracerSmoke"
key_links:
- from: "../sm-mfa-plugin/plugin.go"
to: "../fonoteka.go/plugins/golem15/user/classes/login_gate.go"
via: "Boot adds the mfa gate to the published *userclasses.LoginGates"
pattern: "LoginGates"
- from: "../sm-mfa-plugin/classes/login.go"
to: "../fonoteka.go/plugins/golem15/user/controllers/registration.go"
via: "FinishLogin, IssueToken, APIArray and FireLoginEvent after a verified factor"
pattern: "FireLoginEvent"
- from: "../sm-mfa-plugin/plugin.go"
to: "../fonoteka.go/plugins/golem15/user/classes/events.go"
via: "PasswordChangedEvent listener deletes trusted devices"
pattern: "Listen\\[\\*userclasses.PasswordChangedEvent\\]"
- from: "../sm-mfa-plugin/routes.go"
to: "../sm-mfa-plugin/buckets.go"
via: "challenge group uses throttle:2fa-verify"
pattern: "throttle:2fa-verify"
prohibitions:
- statement: "No token, code, TOTP secret, password or recovery code value is written to a log line or an error string."
status: unresolved
verification: judgment
- statement: "YAML is parsed with goccy/go-yaml only; the archived YAML v3 module is never imported by plugin code."
status: unresolved
verification: judgment
- statement: "No notification mail on 2FA enable or disable, no per-group exemption and no Winter import (CONTEXT Deferred Ideas); no child-account exemption (D-13)."
status: unresolved
verification: judgment
- statement: "Nothing is pushed and no remote repository is created in this plan (plan 03 asks for the go-ahead)."
status: unresolved
verification: judgment
- statement: "sm-user-plugin is not changed by this plan except to fix a defect a task exposes, in its own commit with its README change, recorded as a deviation."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not in user-story format; no story invented, see plan 01): `sm-user-plugin` has the PHP user plugin's second factors and device login. CONTEXT D-01 puts the second factors in the new shared plugin `sm-mfa-plugin`, which requires the user plugin and registers itself on plan 01's login gate.
This plan's slice (plan 2 of 4, wave 2, "sm-mfa-plugin core"): the whole plugin except WebAuthn. The tracer proves the TOTP path end to end: a user turns TOTP on, the next login is challenged, and the TOTP code completes it. Expansion slices add recovery codes, email codes and the D-18 hardening, then trusted devices, the admin settings screen, revocation on password change and the admin reset service.
## Spec-less probe fallback
spec-less probe fallback skipped: phase has no requirement IDs to probe (visible skip). `must_haves` derive from SC-1..SC-4 and D-01..D-20.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- Layout copies sm-sociallogin-plugin: package `mfa` at the root (plugin.go, routes.go, buckets.go, mail.go, admin_settings.go, admin_permissions.go), `classes`, `controllers`, `models` (with `models/settings/fields.yaml`), `updates`, `config`, `lang`, `views/mail`; README in sociallogin's structure.
- Config keys use PHP's setting names under `golem15.mfa.`: `two_factor_mode`, `two_factor_available_methods`, `two_factor_enforce_groups`, `two_factor_email_code_ttl`, `two_factor_challenge_ttl`, `two_factor_passwordless_login`, `trusted_device_enabled`, `trusted_device_ttl_days`, `two_factor_social_login` (new, D-09), plus `issuer` (default app.name), `failure_cap.max` (10) and `failure_cap.window_minutes` (15) for D-18 A6. The settings row (table `golem15_mfa_settings`, nullable columns, NULL means the config default) overrides per request.
- Settings field types per RESEARCH Q8: `dropdown` for the mode, `taglist` with `mode: array` on a JSONB column (type `models.StringList` with sql.Scanner and driver.Valuer) plus `BeforeValidate` allow-lists for methods (totp, email, webauthn) and enforce groups (existing group codes), `number`, `switch`. Never checkboxlist or radio (Pitfall 19).
- The mode is a typed string with an allow-list; an unknown stored value behaves as `optional` and is refused by validation (the PHP USER-005 type confusion, mapped in plan 04).
- Management routes carry `jwt.auth` per route as PHP's group does, and sit in a group with `throttle:user-api` (hardening beyond PHP: the password-confirming recovery-code routes otherwise have no rate limit); PHP's 401 `{"error":"Unauthorized"}` is the handler's fallback when no principal resolves.
- Five real attempts per challenge (RESEARCH A5; PHP's increment-then-check gave four). The response shapes are identical either way.
- Per-user failure cap (D-18 A6): failures in the window = the sum over the user's challenge rows updated within `failure_cap.window_minutes` of `attempts` for uncompleted rows and `attempts - 1` for completed ones; at `failure_cap.max` or more the gate refuses with Status 401 and the verify endpoints answer their generic 401. Creating a challenge deletes only the user's rows whose expires_at is older than the window, so failures stay counted (PHP deleted expired rows eagerly).
- The email send counter is a new column `email_sends SMALLINT NOT NULL DEFAULT 0` on the challenge table (D-18 A8 needs it; D-16's PHP columns are otherwise unchanged).
- `available_methods` in a challenge lists the user's enabled methods, dropping webauthn when no enabled credential exists (PHP), unfiltered by the global list. Status `enforced` is informational as in PHP (RESEARCH Open Question 2): mode enforced and either no enforce groups or a shared group code.
- Recovery codes are always accepted through `/2fa/recovery`, whatever the available list says (D-05). Recovery input is uppercased, spaces removed, hyphen kept, and capped at 32 characters before any bcrypt compare (Pitfall 9).
- The mail template has no layout header (postcard's default layout; the user plugin's `user` alias is not visible to another plugin, Pitfall 18); vars are passed in both cases (`name`/`Name`, `code`/`Code`, `expiry_minutes`/`ExpiryMinutes`).
- Admin reset clears each method's secret and metadata as it disables it (PHP left them; a reset must not let an old secret return when the method is re-enabled).
- Migration IDs `202610220001`..`202610220005` in this plan and `202610220006` (WebAuthn credentials) in plan 03; they may still change until plan 03's release, after which they are frozen.
- The go.mod requires `git.golem15.com/golem15/sm-user-plugin v0.1.5` and the framework tag sm-sociallogin-plugin requires, with go.work replacing both by the local checkouts (sociallogin precedent); plan 03 moves the user-plugin require to the released tag.
- Context budget (estimate 340k, 3 tasks; accepted under the project's lean-planning rule): if the first context runs low after Task 2, the executor writes `19.6-02-SUMMARY.md` as a draft (`Status: draft (hand-off after Task 2)`) and a fresh agent runs Task 3 from this plan, the draft and the repository on disk.
## Artifacts this phase produces (this plan)
- Repository `../sm-mfa-plugin` (branch master, no remote until plan 03), module `git.golem15.com/golem15/sm-mfa-plugin`, package `mfa`.
- Plugin: `mfa.PluginID` (`golem15.mfa`), `mfa.Plugin` (ID, Requires `golem15.user`, Register, Boot, ConfigFS, LangFS, Routes, Buckets, Migrations, Models, MailTemplatesFS, MailTemplates, AdminFS, Settings, Permissions), `mfa.BucketTwoFactorVerify` (`2fa-verify`), `mfa.SettingsCode` (`mfa`), `mfa.PermissionAccessSettings` (`golem15.mfa.access_settings`).
- classes: `Config`, `LoadConfig(app *backpack.App) (Config, error)`, `Effective` and `LoadEffective(ctx context.Context, app *backpack.App, db *gorm.DB) (Effective, error)`; `Gate` (implements `userclasses.LoginGate`); `NewChallengeToken() (string, error)`, `HashToken(raw string) string` (delegates to `userclasses.HashAPIToken`), `CreateChallenge`, `FindChallenge`, `BeginAttempt`, `CompleteChallenge`, `FailureCapReached`; `NewTOTPSecret`, `TOTPURL`, `VerifyTOTP(secret, code string, at time.Time, lastCounter int64) (int64, bool, error)`; `QRSVG(text string, size int) (string, error)`; `RecoveryCodeAlphabet`, `GenerateRecoveryCodes`, `NormalizeRecoveryCode`, `ConsumeRecoveryCode`, `RemainingRecoveryCodes`; `SendEmailCode`, `MaskEmail`, `VerifyEmailCode`; `IssueTrustedDevice`, `CheckTrustedDevice`, `RevokeTrustedDevices`, `ParseDeviceName`; `EnabledMethods`, `EnableMethod`, `DisableMethod`, `Status`; `CompleteLogin`; `Admin` (implements `userclasses.TwoFactorAdmin`); events `ChallengedEvent`, `FailedEvent`, `RecoveryUsedEvent`, `VerifiedEvent`, `EnabledEvent`, `DisabledEvent`, `RecoveryRegeneratedEvent` with constants `ChallengedEventName` (`golem15.user.2fa.challenged`), `FailedEventName`, `RecoveryUsedEventName`, `VerifiedEventName`, `EnabledEventName`, `DisabledEventName`, `RecoveryRegeneratedEventName`; errors `ErrInvalidChallenge`.
- controllers: `Verify`, `Recovery`, `EmailSend`, `Status`, `TOTPSetup`, `TOTPConfirm`, `TOTPDisable`, `EmailEnable`, `EmailDisable`, `RecoveryCodesCount`, `RecoveryCodesRegenerate`, `TrustedDeviceRevoke`, `TrustedDevicesRevokeAll`.
- models: `TwoFactorMethod`, `TwoFactorChallenge`, `TwoFactorRecoveryCode`, `TrustedDevice`, `Settings`, `StringList`, `LoadSettings(ctx context.Context, db *gorm.DB) (*Settings, error)`, constants `MethodTOTP`, `MethodEmail`, `MethodWebAuthn`, `MethodRecovery`, `ModeDisabled`, `ModeOptional`, `ModeEnforced`, `LangPrefix`.
- migrations `202610220001_create_golem15_user_two_factor_methods`, `202610220002_create_golem15_user_two_factor_challenges`, `202610220003_create_golem15_user_two_factor_recovery_codes`, `202610220004_create_golem15_user_trusted_devices`, `202610220005_create_golem15_mfa_settings`.
- Routes: challenge group `/_user/api/v1/2fa` with `throttle:2fa-verify`: `POST verify`, `POST recovery`, `POST email/send`; management group `/_user/api/v1/2fa` with `throttle:user-api` and `jwt.auth` per route: `GET status`, `POST totp/setup`, `POST totp/confirm`, `DELETE totp`, `POST email/enable`, `DELETE email`, `GET recovery-codes`, `POST recovery-codes/regenerate`, `DELETE trusted-devices/{id}`, `DELETE trusted-devices`.
- Mail templates `golem15.mfa::mail.two_factor_code`, `golem15.mfa::mail.two_factor_code-en`; settings screen `mfa`; permission `golem15.mfa.access_settings`; lang keys `golem15.mfa::lang.settings.*`, `golem15.mfa::lang.permissions.*`, `golem15.mfa::lang.mail.*`.
- Smoke tests `TestQRSVGSmoke`, `TestTracerSmoke`, `TestChallengeSmoke`, `TestTrustedSettingsSmoke`; harness `freshDB`, `bootMFA`.
<objective>
Build sm-mfa-plugin without WebAuthn: a TOTP tracer from setup through challenged login to a verified token, then recovery and email codes with the D-18 hardening, then trusted devices, the settings screen, revocation on password change and the admin reset service.
Purpose: D-01, D-04 (TOTP and QR), D-05, D-06, D-07, D-09 (setting), D-10, D-12, D-13, D-15 (service), D-16 (five tables), D-18 (A6, A8, A9), D-19; SC-1 (TOTP, email, recovery, trusted devices), SC-2 (the challenge step behind 2fa-verify), SC-4 (models and migrations).
Output: local commits in the new repository, each green.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-CONTEXT.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-RESEARCH.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-PATTERNS.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-01-SUMMARY.md
@.planning/notes/2026-10-21-mfa-otp-webauthn.md
Repos and concurrency: this plan creates and writes ../sm-mfa-plugin (a sibling of summercms.go, like ../sm-sociallogin-plugin). It reads ../fonoteka.go/plugins/golem15/user (the sm-user-plugin checkout go.work replaces) and summercms.go and changes neither; a defect in sm-user-plugin that blocks a task is fixed there in its own commit with its README change and recorded as a deviation. No co-author tags; stage by explicit path; one logical change per commit. Keep `go vet ./...` and `go test -short ./...` green in the plugin at every commit. Docker-backed tests skip under -short; run them through `sudo -u golem` if the socket is denied, and record NOT RUN when containers are unreachable (Phase 19.8 saw that on this host).
PHP source of truth (read-only, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user): controllers/TwoFactorApiController.php (validation :37-218, management :229-568, withTrustedDevice :578-588), classes/twofactor/TwoFactorService.php (isEnabledForUser :33-47, getEnabledMethods :54-69, isMethodAvailable :82-90, isEnforcedForUser :95-109, createChallenge and verifyChallenge :116-205, confirmTotp :228-245, enableEmailMethod :250-262, disableMethod :267-294, removeWebAuthnCredential :322-356, trusted devices :455-530, getStatus :549-594, ensureRecoveryCodes :599-608), classes/twofactor/TotpMethod.php (:34-72), classes/twofactor/EmailMethod.php (:52-98), models/TwoFactorChallenge.php (:105-133), models/TwoFactorRecoveryCode.php (:49-112), models/TrustedDevice.php (:36-124), models/Settings.php (:40-63), models/settings/fields.yaml (:90-160), views/mail/two_factor_code.htm, updates/v3.0.0 and v3.1.0 migrations, routes.php (:8-18, :303-337, :372-453). RESEARCH Q4 quotes every body; the PHP files break ties.
<interfaces>
- sm-user-plugin after plan 01 (19.6-01-SUMMARY.md has the final names): `userclasses.LoginGate` (`CheckLogin(ctx, a LoginAttempt) (*LoginGateResult, error)`), `userclasses.LoginGates` (published in the user plugin's Register; add the gate in Boot), `userclasses.LoginAttempt{User, Source, Fields, Request}`, `userclasses.LoginGateResult{Status, Body}`, `userclasses.LoginSourcePassword`, `LoginSourceSocial`, `LoginSourceTwoFactor`, `userclasses.PasswordChangedEvent{User, Tx, Source}`, `userclasses.TwoFactorAdmin`, `userclasses.HashAPIToken`, `userclasses.MailTemplate`, `userclasses.ResolveMailLocale`, `userclasses.UserGroupCodes`, `usercontrollers.FinishLogin`, `IssueToken`, `APIArray`, `RemoteIP`, `FireLoginEvent`, `usermodels.User`, `usermodels.UserGroup` (group codes for the enforce-groups allow-list).
- sm-sociallogin-plugin (template, read-only): plugin.go (assertions, embeds, Register/Boot without DB, typed Publish), routes.go, config.go and classes/config.go (LoadConfig), admin_settings.go, admin_permissions.go, models/settings.go (singleton row 1, LoadSettings, BeforeValidate with cabana.ValidationError), models/registry.go, updates/registry.go (`run` helper), updates/202610200001_create_golem15_sociallogin_settings.go, controllers/helpers.go (writeJSON with Cache-Control no-cache, private), testmain_test.go (flag.Parse, testing.Short, postgres:16-alpine, freshDB), harness_test.go (bootSocialWith: freshDB, lagoon.Use, httptest.NewUnstartedServer, temp config dir with app.yaml, party.Activate; blank import of the user plugin), go.mod, go.work, README.md.
- Other analogs: ../sm-oauthserver-plugin/buckets.go (:29-111, per-IP bucket with surf.ClientIP and surf.TrustedProxies), ../sm-paymentgateway-plugin/mail.go (MailTemplatesFS and MailTemplates), ../sm-chatbots-plugin/boot.go (:82-98 typed listener using ev.Tx), ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (writeValidation :946, sendTemplateVars and mailVars, bcrypt cost read :102-105), ../fonoteka.go/plugins/golem15/user/controllers/api_tokens.go (apiTokenDeps), ../fonoteka.go/plugins/golem15/user/views/mail/restore-en.htm (template front matter).
- Framework: `bouncer.HashPassword(cost, plain)`, `bouncer.CheckPassword(hash, plain)`, `bouncer.User(ctx)`, `lagoon.Encrypted`, `lagoon.Validate`, `wire.Time`, `wire.Slice`, `wire.WriteJSON`, `surf.Bucket{Name, Max, Decay, Key}`, `surf.ClientIP`, `surf.TrustedProxies`, `postcard.Mailer`, `cabana.ValidationError`, `pact.SettingsItem`, `app.Events.Fire`, `app.Events.Listen`.
- Libraries (after plan 01 Task 5 approval only): `github.com/pquerna/otp` (`totp.Generate`, `totp.GenerateCodeCustom`, `hotp.ValidateCustom`, `otp.Key.URL()`), `github.com/boombuler/barcode/qr` (`qr.Encode(text, qr.M, qr.Auto)`).
- RESEARCH Code Examples 1-4, Patterns 2, 5, Pitfalls 1, 4, 5, 9, 10, 11, 12, 17, 18, 19, 20.
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: a user turns TOTP on through setup (QR as SVG) and confirm, the next password login answers the challenge instead of a token, and the current TOTP code completes it with {token, user} exactly once behind the 2fa-verify bucket</name>
<precondition>19.6-01-SUMMARY.md records the Task 5 reply "approved"; `test ! -e ../sm-mfa-plugin` exits 0 or the directory holds only this plan's earlier work.</precondition>
<reversibility rating="costly">D-01: the module path, plugin ID golem15.mfa, the /_user/api/v1/2fa routes and the golem15.mfa config keys become what applications' summer.yaml, config and frontends reference once plan 03 releases the plugin.</reversibility>
<files>../sm-mfa-plugin/.gitignore, ../sm-mfa-plugin/go.mod, ../sm-mfa-plugin/go.sum, ../sm-mfa-plugin/go.work, ../sm-mfa-plugin/plugin.go, ../sm-mfa-plugin/routes.go, ../sm-mfa-plugin/buckets.go, ../sm-mfa-plugin/config/config.yaml, ../sm-mfa-plugin/lang/en/lang.yaml, ../sm-mfa-plugin/lang/pl/lang.yaml, ../sm-mfa-plugin/classes/config.go, ../sm-mfa-plugin/classes/gate.go, ../sm-mfa-plugin/classes/challenge.go, ../sm-mfa-plugin/classes/login.go, ../sm-mfa-plugin/classes/totp.go, ../sm-mfa-plugin/classes/qr.go, ../sm-mfa-plugin/classes/methods.go, ../sm-mfa-plugin/classes/events.go, ../sm-mfa-plugin/controllers/helpers.go, ../sm-mfa-plugin/controllers/challenge.go, ../sm-mfa-plugin/controllers/management.go, ../sm-mfa-plugin/models/registry.go, ../sm-mfa-plugin/models/two_factor_method.go, ../sm-mfa-plugin/models/two_factor_challenge.go, ../sm-mfa-plugin/updates/registry.go, ../sm-mfa-plugin/updates/202610220001_create_golem15_user_two_factor_methods.go, ../sm-mfa-plugin/updates/202610220002_create_golem15_user_two_factor_challenges.go, ../sm-mfa-plugin/testmain_test.go, ../sm-mfa-plugin/harness_test.go, ../sm-mfa-plugin/tracer_smoke_test.go, ../sm-mfa-plugin/README.md</files>
<read_first>19.6-01-SUMMARY.md (Task 5 reply, final user-plugin identifiers), .planning/notes/2026-10-21-mfa-otp-webauthn.md, ../sm-sociallogin-plugin/plugin.go, ../sm-sociallogin-plugin/routes.go, ../sm-sociallogin-plugin/config.go, ../sm-sociallogin-plugin/classes/config.go, ../sm-sociallogin-plugin/config/config.yaml, ../sm-sociallogin-plugin/go.mod, ../sm-sociallogin-plugin/go.work, ../sm-sociallogin-plugin/testmain_test.go, ../sm-sociallogin-plugin/harness_test.go, ../sm-sociallogin-plugin/controllers/helpers.go, ../sm-sociallogin-plugin/updates/registry.go, ../sm-sociallogin-plugin/models/registry.go, ../sm-sociallogin-plugin/README.md, ../sm-oauthserver-plugin/buckets.go, ../fonoteka.go/plugins/golem15/user/classes/login_gate.go, ../fonoteka.go/plugins/golem15/user/controllers/registration.go, ../fonoteka.go/plugins/golem15/user/updates/202610040001_create_api_tokens.go, ../fonoteka.go/plugins/golem15/user/models/api_token.go, modules/lagoon/encrypted.go, 19.6-RESEARCH.md (Q1, Q2, Q4 login challenge, challenge endpoints, status, TOTP, tables; Patterns 1-2; Code Examples 2-4; Pitfalls 4, 5, 17, 20), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/classes/twofactor/TotpMethod.php, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/TwoFactorApiController.php (:37-66, :229-300)</read_first>
<action>Repository (D-01): `git init -b master ../sm-mfa-plugin`, no remote. go.mod: `module git.golem15.com/golem15/sm-mfa-plugin`, `go 1.27.0`, the framework and sm-user-plugin requires as this plan's Decisions state, then `go get github.com/pquerna/otp@v1.5.0 github.com/boombuler/barcode@v1.1.0` and `go mod tidy` (go-webauthn is installed in plan 03). go.work: `use .`, replace the framework with ../summercms.go and sm-user-plugin with ../fonoteka.go/plugins/golem15/user (sociallogin's go.work). .gitignore for test binaries and coverage files.
plugin.go (Pitfall 20): `PluginID = "golem15.mfa"`, interface assertions (party.Plugin, pact.HasConfig, pact.HasLang, pact.HasRoutes, pact.HasMigrations, pact.HasModels, surf.BucketProvider), `//go:embed config` and `lang`, Requires `golem15.user`, Register stores the app, Boot runs LoadConfig (config only) and adds `&classes.Gate{App: app}` to the `*userclasses.LoginGates` it looks up (an error when the registry is missing, since golem15.user always publishes it), `init() { party.Register(&Plugin{}) }`. buckets.go: `BucketTwoFactorVerify = "2fa-verify"`, Max 10, Decay one minute, keyed `"2fa-verify-ip:" + surf.ClientIP(r, trusted)` with trusted proxies from the app config (oauthserver's per-IP bucket). config/config.yaml with the oauthserver header style (each key's SUMMER_GOLEM15__MFA__... env form) and the defaults listed in Decisions; classes/config.go `Config`, `LoadConfig`, and `Effective`/`LoadEffective` returning the config values for now (Task 3 overlays the settings row without changing the signature).
Migrations (raw SQL, D-16, RESEARCH Q4 tables): `golem15_user_two_factor_methods` (id BIGSERIAL PK, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, method VARCHAR(20) NOT NULL, is_enabled BOOLEAN NOT NULL DEFAULT TRUE, secret TEXT NULL, metadata JSONB NULL, last_used_at TIMESTAMPTZ NULL, created_at, updated_at, UNIQUE (user_id, method), index (user_id, is_enabled)); `golem15_user_two_factor_challenges` (id BIGSERIAL PK, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, token VARCHAR(64) NOT NULL UNIQUE (SHA-256 hex, D-06), method VARCHAR(20) NULL, code VARCHAR(255) NULL, attempts SMALLINT NOT NULL DEFAULT 0, max_attempts SMALLINT NOT NULL DEFAULT 5, email_sends SMALLINT NOT NULL DEFAULT 0, ip_address VARCHAR(45) NULL, user_agent TEXT NULL, expires_at TIMESTAMPTZ NOT NULL, completed_at TIMESTAMPTZ NULL, created_at, updated_at, index (user_id, expires_at)); rollbacks drop them. Models with explicit column tags: `TwoFactorMethod` (Secret `lagoon.Encrypted` with `json:"-"`, Metadata as a JSON map), `TwoFactorChallenge` (TokenHash `gorm:"column:token"` `json:"-"`, Code `json:"-"`); method and mode constants in models.
classes: `NewChallengeToken` (48 crypto/rand bytes, base64url, 64 characters) and `HashToken` (userclasses.HashAPIToken). `CreateChallenge(ctx, db, user, ip, userAgent, ttl)` deletes the user's rows whose expires_at is older than the failure window, inserts a row with the hash and returns the plaintext token and the row. `FindChallenge(ctx, db, token)` returns the row only when uncompleted, unexpired and attempts below max. `BeginAttempt(ctx, db, token)` is RESEARCH Code Example 2's guarded UPDATE (`SET attempts = attempts + 1, updated_at = NOW() WHERE token = ? AND completed_at IS NULL AND expires_at > NOW() AND attempts < max_attempts`) returning the row or `ErrInvalidChallenge` when RowsAffected is 0; `CompleteChallenge(ctx, tx, id)` is the guarded `SET completed_at = NOW() WHERE id = ? AND completed_at IS NULL` that fails unless exactly one row changed. totp.go: `NewTOTPSecret(issuer, account)` through `totp.Generate` (pquerna defaults: SHA1, 6 digits, 30 s, 20-byte secret), `TOTPURL`, and `VerifyTOTP(secret, code, at, lastCounter)` per RESEARCH Code Example 3 (counters at-1, at, at+1 with `hotp.ValidateCustom`; a match at or below lastCounter is a replay and fails; returns the matched counter). qr.go `QRSVG(text, size)` per RESEARCH Code Example 4 (qr.Encode level M, 4-module quiet zone, one path of unit squares, width and height = size, viewBox over the module grid, crispEdges). methods.go: `EnabledMethods(ctx, db, userID)` (PHP getEnabledMethods), `EnableMethod`, minimal `Status` (enabled, enforced, methods; Task 3 completes the PHP shape). gate.go `Gate.CheckLogin`: Source password (social waits for Task 3's toggle; passkey, two_factor and device never challenge); mode disabled → nil; no enabled method → nil; else CreateChallenge with the configured TTL, fire ChallengedEvent, return Status 200 with Body `{"two_factor_required": true, "challenge_token": token, "available_methods": wire.Slice(methods), "expires_at": wire.Time(expires)}`. login.go `CompleteLogin(ctx, app, db, r, user)`: `usercontrollers.FinishLogin` (restores a deactivated account only now, D-18 A10), `IssueToken`, `APIArray`, `FireLoginEvent` with `userclasses.LoginSourceTwoFactor` (logged on error), returning `{"token", "user"}`. events.go: the typed structs and PHP names for the events this plan fires (Artifacts list), each carrying User, Method and Tx where a transaction exists.
controllers (handler factories over *backpack.App; helpers.go copies sociallogin's writeJSON and the user plugin's writeValidation): `TOTPSetup` (jwt.auth; 403 `{"error":"TOTP is not available."}` unless PHP isMethodAvailable holds: mode not disabled and totp in the available methods; answers `{secret, qr_code_svg, qr_code_url}` with issuer `golem15.mfa.issuer` or app.name and account the user's email); `TOTPConfirm` (fields `secret` required string and base32 with length 16 to 64, `code` required string size 6, PHP 422 shapes; VerifyTOTP at now with lastCounter -1 else 422 `{"error":"Invalid verification code."}`; upsert the totp row enabled with the encrypted secret and metadata `{"last_counter": n}`; fire EnabledEvent; answer `{"message":"TOTP enabled successfully.","status":...}`); `Status` (minimal for now); `Verify` (unauthenticated; validation `challenge_token` required|string|size:64, `method` required|string|in:totp,email, `code` required|string|min:6|max:6 with PHP's 422 `{error: first message, errors}`; BeginAttempt else 401 `{"error":"Invalid verification code.","two_factor_failed":true}`; method totp: the user's enabled totp row, VerifyTOTP against its last_counter; failure fires FailedEvent and answers that 401; success runs in one transaction: CompleteChallenge, store the new last_counter and last_used_at, then CompleteLogin and VerifiedEvent after the login event; answer `{token, user}`; method email answers the same 401 until Task 2). routes.go: `r.Group("/_user/api/v1/2fa", surf.Use("throttle:2fa-verify"), ...)` with `g.Post("/verify", ...)`; `r.Group("/_user/api/v1/2fa", surf.Use("throttle:user-api"), ...)` with `g.Get("/status", ..., "jwt.auth")`, `g.Post("/totp/setup", ..., "jwt.auth")`, `g.Post("/totp/confirm", ..., "jwt.auth")`.
Tests: testmain_test.go and harness_test.go copied from sm-sociallogin-plugin and renamed (database `golem15_mfa`, `bootMFA(t, env map[string]string) (*backpack.App, *httptest.Server)` activating golem15.user and golem15.mfa with the memory cooler and a capturing mailer; helpers to insert an activated user, log in and call with a bearer). tracer_smoke_test.go: `TestQRSVGSmoke` (no database, -short: QRSVG of an otpauth URL has width and height 200 and a viewBox; parsing the path's unit squares back gives exactly the dark modules of `qr.Encode` for the same text; an empty text returns an error). `TestTracerSmoke` (Postgres): a user calls totp/setup and gets a 32-character base32 secret, an `<svg` qr_code_svg and an otpauth URL with issuer and account; totp/confirm with the code from `totp.GenerateCode(secret, now)` answers the PHP message; the stored secret column differs from the secret (ciphertext); POST /_user/api/v1/login answers 200 with two_factor_required true, a 64-character challenge_token, available_methods ["totp"], an expires_at ending in +00:00 and no token; the stored challenge token column equals the SHA-256 hex of challenge_token; verify with the current code answers 200 with a token and user; verify again with the same challenge answers 401 with the PHP body; a fresh challenge verified with the already used code answers 401 (replay); eleven POSTs to /2fa/verify from one IP within a minute end with a 429.
README in sociallogin's structure (H1 golem15.mfa, summary, import line, module/package/plugin-ID line, Overview, Features, Routes, Configuration, API reference, Security notes, Dependencies, Testing), neutral wording, stating what exists after this task. Commit: `feat: TOTP second factor through the login gate`.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short -run '^TestQRSVGSmoke$' -v . 2&gt;&amp;1 | grep -q -- '^--- PASS: TestQRSVGSmoke'</automated>
<fails_when>Non-zero exit: vet or a short test is red, or no "--- PASS: TestQRSVGSmoke" line (the SVG does not reproduce the QR matrix or the test did not run).</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -run '^TestTracerSmoke$' -v . 2&gt;&amp;1 | grep -q -- '^--- PASS: TestTracerSmoke'</automated>
<fails_when>No "--- PASS: TestTracerSmoke" line: the end-to-end TOTP path failed, or the test skipped because Docker was unreachable (rerun through sudo -u golem; never record a skip as passed).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '^module git.golem15.com/golem15/sm-mfa-plugin$' ../sm-mfa-plugin/go.mod` prints 1, `grep -c 'github.com/pquerna/otp v1.5.0' ../sm-mfa-plugin/go.mod` prints 1 and `grep -c 'github.com/boombuler/barcode v1.1.0' ../sm-mfa-plugin/go.mod` prints 1.
- `NL=$(git log --diff-filter=A --format=%ct -- .planning/notes/2026-10-21-mfa-otp-webauthn.md) && ML=$(git -C ../sm-mfa-plugin log --reverse --format=%ct -S 'pquerna/otp' -- go.mod) && N=$(printf '%s\n' "$NL" | tail -1) && M=$(printf '%s\n' "$ML" | head -1) && test -n "$N" && test -n "$M" && test "$N" -le "$M"` exits 0 (the note was committed before the library was required).
- `grep -c '"golem15.mfa"' ../sm-mfa-plugin/plugin.go` prints at least 1 and `grep -c 'throttle:2fa-verify' ../sm-mfa-plugin/routes.go` prints 1.
- `grep -c 'attempts < max_attempts' ../sm-mfa-plugin/classes/challenge.go` prints at least 1 and `grep -c 'completed_at IS NULL' ../sm-mfa-plugin/classes/challenge.go` prints at least 2.
- `grep -c 'golem15_user_two_factor_challenges' ../sm-mfa-plugin/updates/202610220002_create_golem15_user_two_factor_challenges.go` prints at least 2 and `grep -c 'email_sends' ../sm-mfa-plugin/updates/202610220002_create_golem15_user_two_factor_challenges.go` prints at least 1.
- `R=$(git -C ../sm-mfa-plugin remote) && test -z "$R"` exits 0 (no remote yet).
- `D=$(go -C ../sm-mfa-plugin list -deps .) && test "$(printf '%s\n' "$D" | grep -c '^gopkg.in/yaml')" -eq 0` exits 0.
</acceptance_criteria>
<done>A user can turn on TOTP with a scannable QR code, and their next login asks for the code and completes only with a valid, unused one.</done>
</task>
<task type="auto">
<name>Task 2: Recovery codes and email codes: the first set is shown once, a code works once, regenerate replaces the set, email codes go only to users who enabled them, and a per-user failure cap stops brute force across challenges</name>
<files>../sm-mfa-plugin/models/two_factor_recovery_code.go, ../sm-mfa-plugin/models/registry.go, ../sm-mfa-plugin/updates/202610220003_create_golem15_user_two_factor_recovery_codes.go, ../sm-mfa-plugin/updates/registry.go, ../sm-mfa-plugin/classes/recovery.go, ../sm-mfa-plugin/classes/email.go, ../sm-mfa-plugin/classes/challenge.go, ../sm-mfa-plugin/classes/gate.go, ../sm-mfa-plugin/classes/methods.go, ../sm-mfa-plugin/classes/events.go, ../sm-mfa-plugin/controllers/challenge.go, ../sm-mfa-plugin/controllers/management.go, ../sm-mfa-plugin/routes.go, ../sm-mfa-plugin/mail.go, ../sm-mfa-plugin/plugin.go, ../sm-mfa-plugin/views/mail/two_factor_code.htm, ../sm-mfa-plugin/views/mail/two_factor_code-en.htm, ../sm-mfa-plugin/lang/en/lang.yaml, ../sm-mfa-plugin/lang/pl/lang.yaml, ../sm-mfa-plugin/challenge_smoke_test.go, ../sm-mfa-plugin/README.md</files>
<read_first>../sm-mfa-plugin classes and controllers from Task 1, ../sm-paymentgateway-plugin/mail.go, ../fonoteka.go/plugins/golem15/user/plugin.go (MailTemplates), ../fonoteka.go/plugins/golem15/user/views/mail/restore-en.htm, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (sendTemplateVars, mailVars, bcrypt cost read), ../fonoteka.go/plugins/golem15/user/classes/mail.go, modules/postcard/templates.go (:115-199 ownership and locale rules), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/models/TwoFactorRecoveryCode.php (:49-112), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/classes/twofactor/EmailMethod.php, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/views/mail/two_factor_code.htm, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/classes/twofactor/TwoFactorService.php (:116-205, :228-294, :599-608), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/TwoFactorApiController.php (:74-138, :300-560), 19.6-RESEARCH.md (Q3, Q4 recovery codes, email method, mail template, method enable/disable; Pitfalls 1, 9, 10, 11, 12)</read_first>
<action>Per D-05, D-06, D-07 (last method), D-18 A6/A8/A9, D-19.
Recovery codes: migration `golem15_user_two_factor_recovery_codes` (id BIGSERIAL PK, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, code VARCHAR(255) NOT NULL, used_at TIMESTAMPTZ NULL, created_at TIMESTAMPTZ NULL, no updated_at, index (user_id, used_at)) and model. classes/recovery.go: `RecoveryCodeAlphabet = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"`; `GenerateRecoveryCodes(ctx, tx, userID, cost)` deletes the user's codes and inserts 10 new ones (each two 5-character parts drawn with crypto/rand without modulo bias, joined by a hyphen, stored as `bouncer.HashPassword(cost, code)` with cost from `golem15.user.password.bcrypt_cost`, default 10) and returns the plaintexts; `NormalizeRecoveryCode` (PHP: uppercase, spaces removed, hyphen kept; empty or longer than 32 characters is no match); `ConsumeRecoveryCode(ctx, tx, userID, input)` compares against the unused codes with `bouncer.CheckPassword`, stops at the first match and marks it with the guarded `UPDATE ... SET used_at = NOW() WHERE id = ? AND used_at IS NULL` (a lost race is no match); `RemainingRecoveryCodes`. Enabling (TOTPConfirm here, EmailEnable below) runs PHP's ensureRecoveryCodes inside its transaction: when the user has no unused code, generate a set and add `recovery_codes` (the 10 plaintexts) to the response (D-19); otherwise the body is PHP's.
Challenge endpoints: `Recovery` (validation `challenge_token` required|string|size:64, `code` required|string; BeginAttempt; ConsumeRecoveryCode; success → CompleteChallenge, RecoveryUsedEvent with the remaining count, CompleteLogin, VerifiedEvent with method recovery; failure → FailedEvent and 401 `{"error":"Invalid recovery code.","two_factor_failed":true}`). Email (classes/email.go): `MaskEmail` (PHP: first character, `*` for each of the remaining but last, last character, `@domain`; a local part of 2 or fewer characters gives first character plus `***`); `SendEmailCode(ctx, app, db, challenge, user)`: the user's email method must be enabled (A8); a code `fmt.Sprintf("%06d", n)` with n uniform in [0, 999999] from crypto/rand; one guarded `UPDATE ... SET code = ?, method = 'email', email_sends = email_sends + 1, updated_at = NOW() WHERE id = ? AND completed_at IS NULL AND expires_at > NOW() AND email_sends < 3` with the bcrypt hash (RowsAffected 0 → ErrInvalidChallenge); mail `userclasses.MailTemplate("golem15.mfa::mail.two_factor_code", userclasses.ResolveMailLocale(ctx, user))` through `postcard.Mailer` with vars name, code and expiry_minutes = min(two_factor_email_code_ttl, remaining challenge minutes rounded up, at least 1) in both cases (A9); never log the code. `EmailSend` controller: validation (PHP: 422 `{error}` without `errors`), FindChallenge else 401 `{"error":"Invalid or expired challenge."}`, user missing → 401 `{"error":"Invalid challenge."}`, email method not enabled, send cap reached or cap A6 reached → 401 `{"error":"Invalid or expired challenge."}`; success `{"message":"Verification code sent.","email":<masked>}`. `Verify` method email: after BeginAttempt, the email method must be enabled and the row's method email with a code (else 401 `{"error":"Invalid or expired challenge."}`, A8); a bcrypt match completes as TOTP does, a mismatch fires FailedEvent and answers PHP's invalid-code 401. mail.go embeds views/mail and lists both template names; plugin.go asserts pact.HasMailTemplates. Templates: two_factor_code-en.htm with PHP's subject "Your verification code" and body (Hi {{ .name }}, the code in bold, "This code expires in {{ .expiry_minutes }} minutes.", the secure-your-account sentence); two_factor_code.htm the same in Polish (the unsuffixed template is the Polish one, as in the user plugin); no layout header.
Failure cap (D-18 A6, Pitfall 1): `FailureCapReached(ctx, db, userID, now)` with the formula in this plan's Decisions; the gate returns Status 401 with `{"error":"Invalid or expired challenge."}` and creates no challenge when reached; Verify, Recovery and EmailSend check it after resolving the challenge's user and answer their generic 401 (the attempt still counts).
Management: `EmailEnable` (403 `{"error":"Email 2FA is not available."}` unless available; upsert enabled; ensureRecoveryCodes with D-19; EnabledEvent; `{"message":"Email 2FA enabled.","status":...}`), `EmailDisable` and `TOTPDisable` through `DisableMethod(ctx, tx, user, method)`: set is_enabled false and fire DisabledEvent; when no enabled method remains, delete the user's recovery codes (PHP) and trusted devices (D-07; the trusted-device table arrives in Task 3, so Task 3 extends this call) in the same transaction; answers `{"message":"TOTP disabled.","status":...}` and `{"message":"Email 2FA disabled.","status":...}`. `RecoveryCodesCount` (GET recovery-codes, `password` query checked with bouncer.CheckPassword against the user's hash else 403 `{"error":"Password confirmation required."}`; `{"recovery_codes_remaining":N,"message":"Recovery codes cannot be viewed after creation. Regenerate to get new codes."}`), `RecoveryCodesRegenerate` (POST with `password`; same 403; GenerateRecoveryCodes in one transaction, RecoveryRegeneratedEvent; `{"recovery_codes":[...],"message":"Save these codes securely. They will not be shown again."}`). Routes: challenge group `g.Post("/recovery", ...)`, `g.Post("/email/send", ...)`; management group `g.Delete("/totp", ...)`, `g.Post("/email/enable", ...)`, `g.Delete("/email", ...)`, `g.Get("/recovery-codes", ...)`, `g.Post("/recovery-codes/regenerate", ...)`, each with jwt.auth.
README (same commit): Features (recovery codes, email codes, failure cap, first set shown once), Routes, Configuration (failure_cap keys, email TTL), Security notes (bcrypt cost per wrong recovery code, email only when enabled). Smoke test challenge_smoke_test.go `TestChallengeSmoke` (Postgres) with subtests: `recovery_first_set` (totp/confirm returns 10 codes matching `^[2-9A-HJKMNP-Z]{5}-[2-9A-HJKMNP-Z]{5}$`; a second enable of email answers without recovery_codes; the stored codes are bcrypt hashes); `recovery_once` (a code completes a challenge; the same code on a new challenge answers the PHP 401; GET recovery-codes with the password reports 9; without the password 403); `regenerate` (old codes stop working, new ones work); `email_only_when_enabled` (a TOTP-only user: email/send answers 401 Invalid or expired challenge and no mail is sent; after email/enable, send answers the masked email, the captured mail uses the -en template for an English user with a 6-digit code and expiry_minutes at most 5, and verify method email with that code returns a token); `email_send_cap` (the fourth send on one challenge answers 401); `five_attempts` (five wrong codes on one challenge, then the correct code answers 401); `parallel_verify` (20 goroutines verify the same challenge with the correct code: exactly one 200); `failure_cap` (11 wrong verifications across three challenges, then login answers 401 {"error":"Invalid or expired challenge."} and creates no row; moving the rows' updated_at back past the window lets login challenge again); `disable_last_method` (DELETE totp on a TOTP-only user deletes the recovery codes). Commit: `feat: recovery codes, email codes and the per-user failure cap`.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./...</automated>
<fails_when>Non-zero exit: vet or a short test is red (for example the mail templates fail postcard's ownership check at registration).</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -run '^(TestTracerSmoke|TestChallengeSmoke)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 2: a challenge subtest failed, the tracer regressed, or a test skipped because Docker was unreachable (rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '23456789ABCDEFGHJKMNPQRSTUVWXYZ' ../sm-mfa-plugin/classes/recovery.go` prints at least 1 and `grep -c 'used_at IS NULL' ../sm-mfa-plugin/classes/recovery.go` prints at least 1.
- `grep -c 'email_sends < 3' ../sm-mfa-plugin/classes/email.go` prints 1.
- `grep -c 'golem15.mfa::mail.two_factor_code' ../sm-mfa-plugin/mail.go` prints 2 and `grep -c 'Your verification code' ../sm-mfa-plugin/views/mail/two_factor_code-en.htm` prints 1.
- `grep -c 'func FailureCapReached' ../sm-mfa-plugin/classes/challenge.go` prints 1 and `grep -c 'FailureCapReached(' ../sm-mfa-plugin/classes/gate.go` prints at least 1.
- `grep -c '"recovery_codes"' ../sm-mfa-plugin/controllers/management.go` prints at least 2 (regenerate and the D-19 first set).
- `grep -c '"Invalid recovery code."' ../sm-mfa-plugin/controllers/challenge.go` prints 1 and `grep -c '"Password confirmation required."' ../sm-mfa-plugin/controllers/management.go` prints at least 1.
</acceptance_criteria>
<done>A user can sign in with a recovery code or an emailed code when they set those up, every code works once, and guessing is stopped per user across challenges and IP addresses.</done>
</task>
<task type="auto">
<name>Task 3: Trusted devices skip the challenge until a password change or the last method goes, an administrator configures 2FA in a settings screen without restarting, and the Users screen's Reset 2FA clears a user's second factors</name>
<files>../sm-mfa-plugin/models/trusted_device.go, ../sm-mfa-plugin/models/settings.go, ../sm-mfa-plugin/models/string_list.go, ../sm-mfa-plugin/models/settings/fields.yaml, ../sm-mfa-plugin/models/registry.go, ../sm-mfa-plugin/updates/202610220004_create_golem15_user_trusted_devices.go, ../sm-mfa-plugin/updates/202610220005_create_golem15_mfa_settings.go, ../sm-mfa-plugin/updates/registry.go, ../sm-mfa-plugin/classes/trusted.go, ../sm-mfa-plugin/classes/config.go, ../sm-mfa-plugin/classes/gate.go, ../sm-mfa-plugin/classes/methods.go, ../sm-mfa-plugin/classes/admin.go, ../sm-mfa-plugin/controllers/challenge.go, ../sm-mfa-plugin/controllers/management.go, ../sm-mfa-plugin/routes.go, ../sm-mfa-plugin/admin_settings.go, ../sm-mfa-plugin/admin_permissions.go, ../sm-mfa-plugin/plugin.go, ../sm-mfa-plugin/lang/en/lang.yaml, ../sm-mfa-plugin/lang/pl/lang.yaml, ../sm-mfa-plugin/trusted_settings_smoke_test.go, ../sm-mfa-plugin/README.md</files>
<read_first>../sm-mfa-plugin classes, controllers and models from Tasks 1-2, ../sm-sociallogin-plugin/admin_settings.go, ../sm-sociallogin-plugin/admin_permissions.go, ../sm-sociallogin-plugin/models/settings.go, ../sm-sociallogin-plugin/models/settings/fields.yaml, ../sm-sociallogin-plugin/updates/202610200001_create_golem15_sociallogin_settings.go, ../sm-sociallogin-plugin/settings_smoke_test.go, modules/cabana/settings.go (:98-103), modules/cabana/form_schema.go (:23-30), modules/cabana/field_taglist.go (:124-139), ../sm-chatbots-plugin/boot.go (:82-98), ../fonoteka.go/plugins/golem15/user/classes/two_factor_admin.go, ../fonoteka.go/plugins/golem15/user/classes/events.go, ../fonoteka.go/plugins/golem15/user/classes/user_groups.go, ../fonoteka.go/plugins/golem15/user/reset_two_factor_smoke_test.go, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/models/TrustedDevice.php, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/classes/twofactor/TwoFactorService.php (:33-47, :95-109, :455-530, :549-594), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/models/Settings.php, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/models/settings/fields.yaml (:90-160), 19.6-RESEARCH.md (Q4 trusted devices, status shape, settings; Q8; Q9; Pattern 5; Pitfalls 2, 19; Assumptions A2, A7)</read_first>
<action>Per D-07, D-09 (setting), D-10, D-12, D-13, D-15, D-16.
Trusted devices: migration `golem15_user_trusted_devices` (id BIGSERIAL PK, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, token VARCHAR(64) NOT NULL UNIQUE (SHA-256 hex), device_name VARCHAR(255) NULL, ip_address VARCHAR(45) NULL, trusted_until TIMESTAMPTZ NOT NULL, last_used_at TIMESTAMPTZ NULL, created_at, updated_at, index (user_id)) and model. classes/trusted.go: `ParseDeviceName(userAgent)` ports PHP's "{browser} on {os}" parse (TrustedDevice.php); `IssueTrustedDevice(ctx, db, user, ip, userAgent, ttlDays)` stores the hash and returns the 64-character token; `CheckTrustedDevice(ctx, db, userID, token, ip)` requires a row of that user with trusted_until in the future, then updates last_used_at and ip_address; `RevokeTrustedDevices(ctx, tx, userID)`. Verify, Recovery (and plan 03's WebAuthn verify) add `trusted_device_token` to the success body only when the request's `trust_device` is truthy and trusted_device_enabled is on (PHP withTrustedDevice). The gate allows (returns nil) when trusted devices are enabled and the attempt's Fields carry a valid `trusted_device_token` for the user (Pitfall 2); social sign-ins carry no fields, so trusted devices never skip a social challenge (A7). DisableMethod's last-method branch now also deletes trusted devices (D-07). plugin.go Boot registers `app.Events.Listen[*userclasses.PasswordChangedEvent](PluginID, ...)` deleting the user's trusted devices on ev.Tx (falling back to the published DB when Tx is nil), returning the delete's error so the password write rolls back on failure. Management routes `g.Delete("/trusted-devices/{id}", ...)` followed directly by `g.Where("id", "[0-9]+")` (Pitfall 17) answering `{"message":"Device revoked.","status":...}` or 404 `{"error":"Device not found."}` scoped by user_id, and `g.Delete("/trusted-devices", ...)` answering `{"message":"All trusted devices revoked.","status":...}`.
Settings (D-12, RESEARCH Q8): migration `golem15_mfa_settings` (id INTEGER PRIMARY KEY, two_factor_mode VARCHAR(16) NULL, two_factor_available_methods JSONB NULL, two_factor_enforce_groups JSONB NULL, two_factor_email_code_ttl INTEGER NULL, two_factor_challenge_ttl INTEGER NULL, two_factor_passwordless_login BOOLEAN NULL, trusted_device_enabled BOOLEAN NULL, trusted_device_ttl_days INTEGER NULL, two_factor_social_login BOOLEAN NULL, created_at, updated_at; inserts row 1) through the `run` helper. models/string_list.go `StringList` ([]string as JSON with Scan and Value). models/settings.go: singleton like sociallogin's (TableName, Fillable, `Rules()` with in:disabled,optional,enforced for the mode and integer ranges 1-60 for the TTLs and 1-365 for the trust days, `BeforeValidate` refusing unknown methods outside totp, email, webauthn and unknown group codes with `cabana.ValidationError`), `LoadSettings(ctx, db)`. models/settings/fields.yaml: tabs General (mode dropdown with three static options, available methods taglist mode array, enforce groups taglist mode array, social login switch with a comment that the frontend's OAuth callback must handle two_factor_required before it is turned on, D-09), Codes (email and challenge TTL numbers), Passkeys (passwordless switch with a comment that plan 03's routes use it), Trusted devices (switch and days). classes/config.go `LoadEffective` overlays each non-NULL settings value on the config per call (D-12: no restart), treating a missing table or DB as config only. admin_settings.go and admin_permissions.go as sociallogin's (Code `mfa`, Category Users, Order 540, permission `golem15.mfa.access_settings` for the developer role). Gate: reads LoadEffective per call; mode disabled exempts everyone (D-13; there is no other exemption); Source social challenges only when two_factor_social_login is on (D-09, default skip).
Status (PHP getStatus, TwoFactorService.php:549-594): `{enabled, enforced, methods, webauthn_credentials: [], recovery_codes_remaining, available_methods (the effective global list), trusted_device_enabled, trusted_devices}`; trusted_devices only when enabled, unexpired, ordered last_used_at DESC, items `{id, device_name (default "Unknown Device"), ip_address, last_used_at, trusted_until, created_at}` with Carbon's JSON form `YYYY-MM-DDTHH:MM:SS.000000Z` in UTC (RESEARCH A2; one helper `carbonJSON`), webauthn_credentials stays an empty list until plan 03. Every management answer that embeds `status` uses this.
Admin reset (D-15): classes/admin.go `Admin{App *backpack.App}` implementing `userclasses.TwoFactorAdmin`: HasTwoFactor is true when the user has an enabled method; Reset, on the given transaction, sets every method row disabled with secret and metadata cleared, deletes recovery codes and trusted devices and fires DisabledEvent with method `all` (plan 03 adds the WebAuthn credentials). Boot publishes it with `app.Publish[userclasses.TwoFactorAdmin](&classes.Admin{App: app})`.
README (same commit): Features (trusted devices, settings screen, social toggle, admin reset, revocation rules), Configuration (every key with its default and env form), Admin section, API reference rows. Smoke test trusted_settings_smoke_test.go `TestTrustedSettingsSmoke` (Postgres) with subtests: `trusted_skip` (trusted devices enabled in config: verify with trust_device true returns trusted_device_token, the stored token column is its SHA-256 hex, the next login with it in the body answers a token directly; another user's token or an expired one is challenged); `trusted_disabled` (with the setting off, trust_device adds no token); `password_change_revokes` (change-password through the user plugin deletes the user's trusted devices; a listener failure rolls the change back); `settings_row_applies` (saving mode disabled in the row makes the next login answer a token without restarting; setting it back to optional challenges again); `social_toggle` (the gate called with Source social returns nil while the toggle is off and a challenge when on); `allow_lists` (BeforeValidate refuses methods ["sms"] and an unknown group code); `status_shape` (status keys exactly as PHP's, trusted device dates in the Carbon form); `admin_reset` (calling the published TwoFactorAdmin on a user with TOTP, codes and a trusted device leaves no enabled method, no codes, no devices, fires one disabled event, and the next login answers a token). Commit: `feat: trusted devices, the settings screen and the admin reset service`.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./...</automated>
<fails_when>Non-zero exit: vet or a short test is red (for example cabana refuses the settings form at boot because of an unsupported field type).</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -run '^(TestTracerSmoke|TestChallengeSmoke|TestTrustedSettingsSmoke)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 3: a trusted-device, settings or reset subtest failed, an earlier smoke regressed, or a test skipped because Docker was unreachable (rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'Listen\[\*userclasses.PasswordChangedEvent\]' ../sm-mfa-plugin/plugin.go` prints 1 and `grep -c 'Publish\[userclasses.TwoFactorAdmin\]' ../sm-mfa-plugin/plugin.go` prints 1.
- `grep -cE 'type: (checkboxlist|radio)' ../sm-mfa-plugin/models/settings/fields.yaml` prints 0 and `grep -c 'mode: array' ../sm-mfa-plugin/models/settings/fields.yaml` prints at least 2.
- `grep -c 'two_factor_social_login' ../sm-mfa-plugin/updates/202610220005_create_golem15_mfa_settings.go` prints at least 1 and `grep -c 'two_factor_social_login' ../sm-mfa-plugin/config/config.yaml` prints at least 1.
- `grep -A1 '"/trusted-devices/{id}"' ../sm-mfa-plugin/routes.go | grep -c 'Where("id", "\[0-9\]+")'` prints 1.
- `grep -c '"trusted_device_token"' ../sm-mfa-plugin/controllers/challenge.go` prints at least 1.
- `grep -cE 'golem15.mfa.access_settings|two_factor_mode|trusted_device_enabled' ../sm-mfa-plugin/README.md` prints at least 3.
- `R=$(git -C ../sm-mfa-plugin remote) && test -z "$R"` exits 0 and `test -z "$(git -C ../sm-mfa-plugin status --porcelain)"` exits 0.
</acceptance_criteria>
<done>Users can trust a device for a month, administrators tune 2FA from the backend with immediate effect, and a password change or a reset by an administrator removes every way around the second factor.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| holder of a correct password to the challenge routes | unauthenticated clients present challenge tokens and guessed codes |
| signed-in user to the management routes | a bearer JWT changes the user's own factors and reads recovery-code counts |
| plugin to the database | tokens, secrets and codes at rest |
| plugin to the mailer | one-time codes leave the system by email |
| administrator to the settings screen and the reset action | global 2FA policy and per-user resets |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6-13 | Spoofing | TOTP and email brute force across challenges and IPs (Pitfall 1) | high | mitigate | 2fa-verify bucket 10/min per IP plus the per-user failure cap counted in the database (10 per 15 min, D-18 A6); five attempts per challenge; TestChallengeSmoke failure_cap and five_attempts |
| T-19.6-14 | Tampering | parallel verifies (attempt bypass, double mint) | high | mitigate | guarded attempt increment and single completion UPDATE (Pattern 2); TestChallengeSmoke parallel_verify yields one token |
| T-19.6-15 | Spoofing | TOTP code replay within the window (Pitfall 5) | medium | mitigate | last accepted counter in metadata, refused at or below; TestTracerSmoke replay |
| T-19.6-16 | Spoofing | email downgrade of a TOTP-only user (Pitfall 10) | high | mitigate | email/send and verify method=email only when the email method is enabled (D-18 A8); TestChallengeSmoke email_only_when_enabled |
| T-19.6-17 | Denial of service | mail bombing through email/send | medium | mitigate | at most 3 sends per challenge in one guarded UPDATE, the bucket and the failure cap |
| T-19.6-18 | Information disclosure | secrets at rest | high | mitigate | SHA-256 hex for challenge and trusted-device tokens, lagoon.Encrypted TOTP secret, bcrypt recovery and email codes (D-06); TestTracerSmoke and TestTrustedSettingsSmoke compare stored columns |
| T-19.6-19 | Information disclosure | logs and error strings | medium | mitigate | handlers log only error types and user ids, never codes, tokens, secrets or passwords; 500 answers are generic |
| T-19.6-20 | Spoofing | stolen trusted-device token | medium | mitigate | stored as SHA-256, bound to the user, 30-day expiry, off by default, revoked on any password change and on the last method's removal, listed and revocable per user (D-07, D-10) |
| T-19.6-21 | Elevation of privilege | management routes touching another user's rows | high | mitigate | every query scoped by the user id from the verified JWT; {id} routes constrained to digits with Where directly after the route; 404 for foreign ids |
| T-19.6-22 | Information disclosure | recovery codes after creation | medium | mitigate | codes shown once (first set D-19, regenerate), listing returns only a count and requires the password; the management group carries throttle:user-api |
| T-19.6-23 | Denial of service | bcrypt CPU per wrong recovery code (Pitfall 9) | low | mitigate | input normalized and capped at 32 characters, stop at the first match, bucket and failure cap |
| T-19.6-24 | Tampering | settings values (USER-005 type confusion, unknown methods or groups) | low | mitigate | typed mode with an allow-list in Rules, BeforeValidate allow-lists for methods and groups, unknown stored mode treated as optional |
| T-19.6-25 | Spoofing | client-chosen TOTP secret at confirm (PHP parity) | low | accept | only for the caller's own account; base32 and length validated; a proof code from the secret is required |
| T-19.6-SC | Tampering | Go module installs | high | mitigate | pquerna/otp v1.5.0 and boombuler/barcode v1.1.0 installed only after plan 01 Task 5's recorded approval (precondition); the archived YAML module is absent from `go list -deps` |
</threat_model>
<verification>
- Task 1: TestQRSVGSmoke (short) and TestTracerSmoke; the note precedes the library require; no remote.
- Task 2: TestChallengeSmoke and TestTracerSmoke.
- Task 3: TestTrustedSettingsSmoke, TestChallengeSmoke and TestTracerSmoke.
- Feedback sampling (19.6-VALIDATION.md): after each commit `go -C ../sm-mfa-plugin vet ./... && go -C ../sm-mfa-plugin test -short ./...`.
</verification>
<success_criteria>
- SC-1 (without WebAuthn): TOTP setup, confirm and disable; email codes; recovery codes; trusted devices, all with PHP's paths and bodies.
- SC-2: login with a second factor answers the challenge; the challenge routes share the 2fa-verify bucket.
- D-18: failure cap, email only when enabled, capped sends, mail expiry within the challenge.
- D-12, D-13, D-15: settings screen with immediate effect, only mode disabled exempts, admin reset service published.
</success_criteria>
<output>
Create `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-02-SUMMARY.md` when done, with the commits in order, the final exported identifiers, config keys and migration IDs (plans 03 and 04 use them), which Docker-backed tests ran or were NOT RUN, and any deviation from this plan's names. Post a short comment on the Phase 19.6 milestone task in g15office (`avoroeb4`); if the tool is unavailable, record that in the SUMMARY.
</output>

View File

@@ -0,0 +1,446 @@
---
phase: 19.6-two-factor-auth-and-device-flow-in-sm-user-plugin
plan: 03
type: execute
wave: 3
depends_on: ["19.6-01", "19.6-02"]
files_modified:
- ../sm-mfa-plugin/go.mod
- ../sm-mfa-plugin/go.sum
- ../sm-mfa-plugin/routes.go
- ../sm-mfa-plugin/config/config.yaml
- ../sm-mfa-plugin/lang/en/lang.yaml
- ../sm-mfa-plugin/lang/pl/lang.yaml
- ../sm-mfa-plugin/classes/config.go
- ../sm-mfa-plugin/classes/webauthn.go
- ../sm-mfa-plugin/classes/passwordless.go
- ../sm-mfa-plugin/classes/gate.go
- ../sm-mfa-plugin/classes/methods.go
- ../sm-mfa-plugin/classes/admin.go
- ../sm-mfa-plugin/classes/events.go
- ../sm-mfa-plugin/controllers/webauthn.go
- ../sm-mfa-plugin/controllers/passwordless.go
- ../sm-mfa-plugin/models/webauthn_credential.go
- ../sm-mfa-plugin/models/registry.go
- ../sm-mfa-plugin/updates/202610220006_create_golem15_user_webauthn_credentials.go
- ../sm-mfa-plugin/updates/registry.go
- ../sm-mfa-plugin/webauthn_authenticator_test.go
- ../sm-mfa-plugin/webauthn_smoke_test.go
- ../sm-mfa-plugin/passwordless_smoke_test.go
- ../sm-mfa-plugin/README.md
- ../sm-sociallogin-plugin/classes/flow.go
- ../sm-sociallogin-plugin/classes/codes.go
- ../sm-sociallogin-plugin/controllers/complete.go
- ../sm-sociallogin-plugin/controllers/register_complete.go
- ../sm-sociallogin-plugin/lang/en/lang.yaml
- ../sm-sociallogin-plugin/lang/pl/lang.yaml
- ../sm-sociallogin-plugin/social_gate_smoke_test.go
- ../sm-sociallogin-plugin/README.md
- ../sm-sociallogin-plugin/go.mod
- ../sm-sociallogin-plugin/go.sum
- ../sm-demo-app/vue-demo-app/app/lib/twoFactor.ts
- ../sm-demo-app/vue-demo-app/app/lib/twoFactor.test.ts
- ../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts
- ../sm-demo-app/vue-demo-app/app/composables/useTwoFactor.ts
- ../sm-demo-app/vue-demo-app/app/pages/login.vue
- ../sm-demo-app/vue-demo-app/app/pages/account/security.vue
- ../sm-demo-app/vue-demo-app/app/components/SiteHeader.vue
- ../sm-demo-app/vue-demo-app/i18n/locales/en.json
- ../sm-demo-app/vue-demo-app/i18n/locales/pl.json
- ../sm-demo-app/.gitmodules
- ../sm-demo-app/plugins/golem15/mfa
- ../sm-demo-app/plugins/golem15/user
- ../sm-demo-app/plugins/golem15/sociallogin
- ../sm-demo-app/vue-demo-app
- ../sm-demo-app/go.mod
- ../sm-demo-app/go.sum
- ../sm-demo-app/go.work
- ../sm-demo-app/summer.yaml
- ../sm-demo-app/plugins.gen.go
- ../sm-demo-app/boot_test.go
- ../sm-demo-app/mfa_test.go
- ../sm-demo-app/config/golem15.mfa.yaml
- ../sm-demo-app/deploy/env.example
- ../sm-demo-app/README.md
- ../sm-bm-app/.gitmodules
- ../sm-bm-app/plugins/golem15/mfa
- ../sm-bm-app/plugins/golem15/user
- ../sm-bm-app/plugins/golem15/sociallogin
- ../sm-bm-app/go.mod
- ../sm-bm-app/go.sum
- ../sm-bm-app/go.work
- ../sm-bm-app/summer.yaml
- ../sm-bm-app/plugins.gen.go
- ../sm-bm-app/mfa_test.go
- ../sm-bm-app/README.md
- ../fonoteka.go/plugins/golem15/user
- ../fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go
- ../fonoteka.go/plugins/golem15/fonoteka/classes/hidden_marshal_test.go
autonomous: false
requirements: [SC-1, SC-2, SC-4, D-01, D-02, D-04, D-05, D-06, D-08, D-09, D-10, D-14, D-16, D-17, D-19, D-20]
user_setup:
- service: git.golem15.com
why: "Create the golem15/sm-mfa-plugin repository and push and tag sm-user-plugin, sm-sociallogin-plugin, sm-mfa-plugin, vue-demo-app, sm-demo-app and sm-bm-app (D-17), only after the Task 4 go-ahead"
dashboard_config:
- task: "Approve the release train in Task 4; the executor runs tea repos create and git push under that approval"
location: "git.golem15.com, organisation golem15"
estimate:
tokens: 360000
raw_tokens: 360000
tasks: 6
confidence: low
must_haves:
truths:
- "D-04, D-08: github.com/go-webauthn/webauthn v0.18.2 is required only by sm-mfa-plugin; POST 2fa/webauthn/register/options and 2fa/webauthn/options answer {options: <go-webauthn's standard base64url CredentialCreation or CredentialAssertion>, challenge}; register takes {attestation, challenge, name?} and webauthn/verify takes {challenge_token, assertion, trust_device?} with the standard RegistrationResponseJSON and AuthenticationResponseJSON shapes; attestation none, timeout 60 s, user verification preferred for second-factor use; RP ID golem15.mfa.webauthn.rp_id or the app.url host, RP name the app name, origins golem15.mfa.webauthn.origins or the app.url and golem15.user.frontend_url origins."
- "D-05, D-06: the challenge's WebAuthn state lives server-side (the expected challenge in the challenge row's code column; registration and passkey sessions in cooler, consumed once with Pull); a client-sent challenge is only a lookup key; register answers PHP's {message, credential:{id, name, created_at}, status} (plus recovery_codes when it created the first set, D-19); webauthn/verify answers {token, user} (plus trusted_device_token when asked and enabled) or 401 {\"error\":\"WebAuthn verification failed.\",\"two_factor_failed\":true}; DELETE webauthn/{id} answers PHP's bodies and the last credential's removal disables the method with the last-method cleanup."
- "D-16: golem15_user_webauthn_credentials keeps PHP's columns (credential_id, public_key, attestation_type, transports, sign_count, name, is_enabled, last_used_at) and adds credential_data (the library's Credential JSON, written back after every assertion with sign count and flags) and passwordless, with a unique index on credential_id; status lists webauthn_credentials as PHP's {id, name, last_used_at, created_at}; admin reset deletes the credentials."
- "D-14: POST /_user/api/v1/2fa/webauthn/passwordless/options and .../verify sit in the 2fa-verify bucket, answer 403 {\"error\":\"Passwordless login is not available.\"} while two_factor_passwordless_login is off, accept only enabled credentials registered as passkeys (resident key required, user verification required at registration and at login, credProps rk reported true), enforce activation, ban and suspension, fire the login event with Source passkey and issue a token without a second challenge."
- "D-09: sm-sociallogin-plugin consults the login gate in signIn before FinishLogin (Source social); with the mfa toggle off it behaves as v0.1.2; with it on, oauth-complete and oauth-register-complete answer 200 with the two_factor_required body instead of {token, user, action, return_to}; a refusing gate result redirects with the two_factor_unavailable message; gate errors fail closed; action=link is never gated."
- "D-20: the demo site (vue-demo-app, mounted in sm-demo-app) lets a signed-in user set up TOTP with the QR code and a confirm code, shows the first recovery codes once, disables TOTP, and answers a login challenge with a TOTP or recovery code; sm-bm-app stays API-only."
- "D-17: after the Task 4 go-ahead the remote operations run in order: sm-user-plugin pushed and tagged, then sm-mfa-plugin (new repository, tagged v0.1.0) and sm-sociallogin-plugin pushed and tagged against it, then vue-demo-app pushed; sm-demo-app and sm-bm-app mount golem15.mfa and bump the user and sociallogin pointers in separate commits; fonoteka.go bumps the user pointer and updates TestNoDeferredRoutesResolve (drops /devices, keeps 2fa) and its user-model count in that commit, does not mount mfa, links no WebAuthn module and is never pushed."
- "D-01: an application without golem15.mfa (fonoteka.go) has no /2fa route, no 2FA table and no go-webauthn, pquerna/otp or boombuler module in its module graph."
artifacts:
- path: "../sm-mfa-plugin/classes/webauthn.go"
provides: "per-request go-webauthn instance, user adapter, ceremony state"
contains: "webauthn.New"
- path: "../sm-mfa-plugin/classes/passwordless.go"
provides: "D-14 passkey login"
contains: "ValidatePasskeyLogin"
- path: "../sm-mfa-plugin/updates/202610220006_create_golem15_user_webauthn_credentials.go"
provides: "golem15_user_webauthn_credentials"
contains: "credential_data"
- path: "../sm-sociallogin-plugin/classes/flow.go"
provides: "D-09 gate in signIn"
contains: "CheckLoginGates"
- path: "../sm-demo-app/vue-demo-app/app/pages/account/security.vue"
provides: "D-20 TOTP setup, recovery codes, disable"
contains: "totp/setup"
- path: "../sm-demo-app/mfa_test.go"
provides: "TestMFAMounted"
contains: "TestMFAMounted"
key_links:
- from: "../sm-mfa-plugin/controllers/webauthn.go"
to: "modules/cooler/cooler.go"
via: "registration state consumed once with cooler.Cache.Pull"
pattern: "Pull\\("
- from: "../sm-sociallogin-plugin/classes/flow.go"
to: "../fonoteka.go/plugins/golem15/user/controllers/login_gates.go"
via: "signIn calls usercontrollers.CheckLoginGates with Source social before FinishLogin"
pattern: "LoginSourceSocial"
- from: "../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts"
to: "/_user/api/v1/2fa/verify"
via: "the login page completes a challenge through verify or recovery"
pattern: "2fa/verify"
- from: "../sm-demo-app/summer.yaml"
to: "../sm-mfa-plugin/plugin.go"
via: "golem15.mfa mounted, plugins.gen.go regenerated"
pattern: "golem15.mfa"
prohibitions:
- statement: "No assertion, attestation, challenge, token, code or library error detail is logged or echoed; registration failures answer fixed reasons."
status: unresolved
verification: judgment
- statement: "The demo renders the server's SVG as an image data URL, never through v-html."
status: unresolved
verification: judgment
- statement: "Nothing is pushed, tagged, created remotely or deployed before the Task 4 reply; nothing is ever force-pushed; fonoteka.go is never pushed; no application is deployed."
status: unresolved
verification: judgment
- statement: "fonoteka.go does not mount golem15.mfa; sm-bm-app's frontend gets no 2FA UI (D-17, D-20)."
status: unresolved
verification: judgment
- statement: "The stale, remote-less ../vue-demo-app checkout is not used; the demo UI is committed in ../sm-demo-app/vue-demo-app (origin golem15/vue-demo-app)."
status: unresolved
verification: judgment
---
<!-- planner-discipline-allow: golem15.mfa -->
<!-- planner-discipline-allow: go-webauthn -->
## Phase Goal
ROADMAP Goal (not in user-story format; no story invented, see plan 01): `sm-user-plugin` has the PHP user plugin's second factors and device login. CONTEXT D-01 puts the second factors in `sm-mfa-plugin`; D-14 adds passkey login, D-09 the social-login gate, D-17 the adoption and D-20 the demo UI.
This plan's slice (plan 3 of 4, wave 3, "WebAuthn and adoption"): the WebAuthn second factor (tracer), passwordless passkey login and the social-login gate, the demo's two-factor screens, then the release go-ahead, the publishing in D-17 order and the adoption in sm-demo-app, sm-bm-app and fonoteka.go.
## Spec-less probe fallback
spec-less probe fallback skipped: phase has no requirement IDs to probe (visible skip). `must_haves` derive from SC-1..SC-4 and D-01..D-20.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- Passwordless paths (D-14 discretion): `POST /_user/api/v1/2fa/webauthn/passwordless/options` (body `{}`) and `POST /_user/api/v1/2fa/webauthn/passwordless/verify` (body `{challenge, assertion}`), in the challenge group with the 2fa-verify bucket. While the setting is off both answer 403 `{"error":"Passwordless login is not available."}` (the "X is not available." wording of PHP's management errors).
- Registration takes an optional `passwordless` boolean (with the setting on) to request a passkey: resident key required, user verification required and the credProps extension. A credential is stored with `passwordless = true` only when it was requested, the authenticator data's UV flag is set and credProps reports rk true; without credProps the credential stays second-factor-only (fail closed).
- User handle: the 8-byte big-endian user id, as PHP's `hex2bin(str_pad(dechex(id), 16, '0', STR_PAD_LEFT))` (RESEARCH A4), so a later import maps; it reveals the numeric id to the authenticator (accepted, T-19.6-34).
- Credential storage: `credential_data` TEXT holding the JSON of `webauthn.Credential` (RESEARCH A3), plus PHP's display columns; written back after every successful assertion (Pitfall 8). A clone warning (non-zero sign counts that do not increase) refuses the login with the generic failure and logs the credential row id.
- Ceremony state (Pitfall 6, Pattern 3): login 2FA stores the base64url challenge in the challenge row's `code` with `method = webauthn` (rebuilt into SessionData at verify); registration and passkey sessions are JSON in `cooler` under `mfa:webauthn:register:<user id>:<challenge>` and `mfa:webauthn:passwordless:-:<challenge>`, TTL 120 s, consumed with `Pull`; the memory driver works within one instance only (A11, README note).
- The RP: `golem15.mfa.webauthn.rp_id` (default the app.url host), `golem15.mfa.webauthn.rp_name` (default app.name), `golem15.mfa.webauthn.origins` (default the app.url origin and the `golem15.user.frontend_url` origin). An IP-address host cannot run a ceremony in browsers (Pitfall 7); the README says so. The demo serves the SPA and the API on one host in production, so the defaults fit; locally (127.0.0.1) WebAuthn is not usable, which D-20 allows.
- Social sign-in (D-09, RESEARCH Q5): the gate runs in `signIn` before `FinishLogin` so a deactivated account is not restored before the second factor (D-18 A10); `CompletionPayload` and `Outcome` gain `Challenge map[string]any`; `Complete` and `RegisterComplete` answer it as 200 JSON (the token cookie option does not apply to it); a non-200 gate result becomes a FlowError with the new message key `oauth.two_factor_unavailable`. Trusted devices never skip a social challenge (A7).
- Demo UI location: CONTEXT D-20 names `../vue-demo-app`, but that sibling is a stale local copy with no remote (2 commits, `32049f7`); the real `golem15/vue-demo-app` repository is the submodule checkout `../sm-demo-app/vue-demo-app` (origin set, `5a8d544`, vitest configured). The UI is committed there and reaches sm-demo-app through a pointer bump. The QR SVG is shown through an `img` data URL. Email codes, WebAuthn and trusted devices in the demo UI are left out (optional in D-20).
- Release (D-17, CONTEXT discretion): one go-ahead (Task 4) covers every remote operation of the phase, including plan 04's possible patch releases only if Task 4's reply says so. Tag numbers are chosen in the reply: sm-user-plugin the next patch after its newest tag on origin (v0.1.6 unless Phase 19.8 released one first), sm-sociallogin-plugin the next patch (v0.1.3), sm-mfa-plugin v0.1.0. No framework tag is needed: sm-mfa-plugin requires the framework version sm-sociallogin-plugin requires (v0.3.1, the first tag with `cooler.Cache.Pull`), so mounting it does not move an application's framework pin; a newer tag only if the code needs one, named in the SUMMARY. sm-demo-app's `TestGoModRequiresTags` needs tagged requires, so a push without tags cannot mount mfa in the demo.
- Concurrency: Phase 19.8 plan 03 Task 7 may be bumping pointers in sm-demo-app and sm-bm-app; before each commit there, `git status --porcelain` is checked for edits to the files this task touches, and the executor stops and reports when one is another session's.
- Context budget (estimate 360k, 6 tasks; accepted under the four-plan decision): Tasks 1-3 run in the first context; the Task 4 checkpoint ends it; a continuation agent runs Tasks 5 and 6 from this plan, the 19.6-01/02 SUMMARYs and the `19.6-03-SUMMARY.md` draft written at the checkpoint (`Status: draft (checkpoint Task 4)`, commits with hashes per repository, NOT RUN notes).
## Artifacts this phase produces (this plan)
- sm-mfa-plugin: module requirement `github.com/go-webauthn/webauthn v0.18.2`; classes `WebAuthnFor(ctx context.Context, app *backpack.App) (*webauthn.WebAuthn, error)`, `UserHandle(id uint) []byte`, `NewWebAuthnUser`, `BeginRegistration`, `FinishRegistration`, `BeginAssertion`, `FinishAssertion`, `BeginPasswordless`, `FinishPasswordless`; models `WebAuthnCredential` (table `golem15_user_webauthn_credentials`); migration `202610220006_create_golem15_user_webauthn_credentials`; controllers `WebAuthnRegisterOptions`, `WebAuthnRegister`, `WebAuthnRemove`, `WebAuthnOptions`, `WebAuthnVerify`, `PasswordlessOptions`, `PasswordlessVerify`; events `WebAuthnRegisteredEvent` (`golem15.user.2fa.webauthn.registered`), `WebAuthnRemovedEvent` (`golem15.user.2fa.webauthn.removed`); config keys `golem15.mfa.webauthn.rp_id`, `golem15.mfa.webauthn.rp_name`, `golem15.mfa.webauthn.origins`; routes `POST /_user/api/v1/2fa/webauthn/options`, `POST .../2fa/webauthn/verify`, `POST .../2fa/webauthn/passwordless/options`, `POST .../2fa/webauthn/passwordless/verify` (2fa-verify bucket) and `POST .../2fa/webauthn/register/options`, `POST .../2fa/webauthn/register`, `DELETE .../2fa/webauthn/{id}` (jwt.auth); test helper `virtualAuthenticator`; smoke tests `TestWebAuthnCeremonySmoke`, `TestWebAuthnSmoke`, `TestPasswordlessSmoke`; remote `git@git.golem15.com:golem15/sm-mfa-plugin.git`, tag `v0.1.0`.
- sm-sociallogin-plugin: field `classes.CompletionPayload.Challenge`, field `classes.Outcome.Challenge`, lang key `golem15.sociallogin::lang.oauth.two_factor_unavailable`, smoke test `TestSocialGateSmoke`, the next patch tag.
- sm-user-plugin: the next patch tag carrying plan 01.
- vue-demo-app: `app/lib/twoFactor.ts` (`isTwoFactorChallenge`, `normalizeRecoveryCode`, `isTotpCode`, `challengeExpired`, `svgDataUrl`, types `TwoFactorChallenge`, `TwoFactorStatus`, `TotpSetup`), `app/composables/useTwoFactor.ts` (`useTwoFactor`), `useDemoAuth` gaining `verifyChallenge` and `recoverChallenge` and a challenge-aware `login`, page `/account/security`, header link, i18n keys `auth.twoFactor.*` and `security.*`.
- sm-demo-app: submodule `plugins/golem15/mfa`, `config/golem15.mfa.yaml`, `TestMFAMounted` (mfa_test.go), plugin row in README; sm-bm-app: submodule `plugins/golem15/mfa`, `TestMFAMounted` (mfa_test.go); fonoteka.go: updated `TestNoDeferredRoutesResolve` and user-model count.
<objective>
Add WebAuthn as a second factor and as passwordless passkey login, put social sign-in through the gate behind its setting, give the demo its two-factor screens, then, under the go-ahead, publish in D-17 order and mount the plugin in the demo and bm applications while fonoteka.go moves to the new user plugin.
Purpose: D-04 (go-webauthn), D-05, D-06, D-08, D-09, D-10 (WebAuthn verify), D-14, D-16 (sixth table), D-17, D-19 (register), D-20; SC-1 (WebAuthn registration and login challenges), SC-4.
Output: commits in sm-mfa-plugin, sm-sociallogin-plugin, vue-demo-app, sm-demo-app, sm-bm-app and fonoteka.go; after the reply the repository, tags and pushes listed in Task 5.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-CONTEXT.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-RESEARCH.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-PATTERNS.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-01-SUMMARY.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-02-SUMMARY.md
Repos and concurrency: this plan writes sm-mfa-plugin (../sm-mfa-plugin), sm-sociallogin-plugin (../sm-sociallogin-plugin, at v0.1.2 `9fd58db`), the vue-demo-app submodule checkout (../sm-demo-app/vue-demo-app, origin golem15/vue-demo-app, at `5a8d544`), sm-demo-app, sm-bm-app and fonoteka.go, and pushes sm-user-plugin from ../fonoteka.go/plugins/golem15/user. Shared plugins are committed and pushed before the pointer bumps in applications (CLAUDE.md, core-plugins-own-repos.md); never stage files inside a submodule from the application repository; use `ssu` or plain `git submodule` for submodule moves. In fonoteka.go stage only `plugins/golem15/user` and the test files this plan names (its feedback and golem pointer moves belong to other sessions); fonoteka.go is never pushed. sm-bm-app has untracked planning files from other sessions: stage by explicit path. No co-author tags; one logical change per commit. Docker-backed tests skip under -short; run them through `sudo -u golem` if the socket is denied; record NOT RUN when containers are unreachable.
PHP source of truth (read-only, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user): controllers/TwoFactorApiController.php (:146-218 WebAuthn challenge, :300-470 WebAuthn management), classes/twofactor/WebAuthnMethod.php (:18-20, :125-142, :170-179, :217-220, :316-321), classes/twofactor/TwoFactorService.php (:322-356 removeWebAuthnCredential, :549-594 status), models/WebAuthnCredential.php, updates/v3.0.0 create_webauthn_credentials_table.php, components/TwoFactor.php (:278, :293 passwordless handlers). Clients: /media/nvme/dev/jz/wavepath.org/vue-wavepath-app/src/types/auth.ts (:157-162 TwoFactorChallenge type).
<interfaces>
- sm-mfa-plugin after plan 02 (19.6-02-SUMMARY.md has the final names): `classes.LoadEffective`, `classes.FindChallenge`, `classes.BeginAttempt`, `classes.CompleteChallenge`, `classes.FailureCapReached`, `classes.CompleteLogin`, `classes.EnableMethod`, `classes.DisableMethod`, `classes.Status`, `classes.IssueTrustedDevice`, `classes.Admin`, the ensure-recovery-codes helper, the events, `controllers` helpers (writeJSON, writeValidation, the jwt principal helper), `models.TwoFactorMethod`, `models.MethodWebAuthn`, routes.go groups.
- go-webauthn v0.18.2 (RESEARCH Q6): `webauthn.New(&webauthn.Config{RPID, RPDisplayName, RPOrigins, AttestationPreference, Timeouts})`, `BeginRegistration(user, opts...)`, `CreateCredential(user, session, parsed)`, `BeginLogin(user, opts...)`, `ValidateLogin(user, session, parsed)`, `BeginDiscoverableLogin(opts...)`, `ValidatePasskeyLogin(handler, session, parsed)`, `protocol.ParseCredentialCreationResponseBytes`, `protocol.ParseCredentialRequestResponseBytes`, `webauthn.WithResidentKeyRequirement(protocol.ResidentKeyRequirementRequired)`, `webauthn.WithUserVerification(protocol.VerificationRequired|VerificationPreferred)`, `webauthn.WithExclusions`, `webauthn.WithAllowedCredentials`, `webauthn.WithConveyancePreference(protocol.PreferNoAttestation)`, `webauthn.WithExtensions`, `webauthn.User` (WebAuthnID, WebAuthnName, WebAuthnDisplayName, WebAuthnCredentials), `webauthn.SessionData`, `webauthn.Credential` (JSON round-trip).
- sm-sociallogin-plugin v0.1.2: classes/flow.go `signIn` :311-324 (FinishLogin then IssueToken and APIArray), `linkIdentity` (never gated), `Outcome`; classes/codes.go `CompletionPayload` :35-40, `PutCompletion`, `PullCompletion`; controllers/complete.go :17-35 (`writeSession`); controllers/register_complete.go; controllers/callback.go (FlowError redirect); classes/messages.go and lang files; tracer_smoke_test.go and harness_test.go (`bootSocial`, fake providers through the Registry).
- sm-user-plugin after plan 01: `usercontrollers.CheckLoginGates`, `userclasses.LoginAttempt`, `userclasses.LoginSourceSocial`, `userclasses.LoginSourcePasskey`, `usercontrollers.FinishLogin`, `IssueToken`, `APIArray`, `FireLoginEvent`, `userclasses.RejectIfThrottled`.
- vue-demo-app (../sm-demo-app/vue-demo-app): app/composables/useDemoAuth.ts (login, register, logout, formErrors, authErrors), app/pages/login.vue (submit, busy, error, next guard, Turnstile widget), app/pages/subscriptions.vue (logged-out notice), app/components/SiteHeader.vue (:9-47 links and logout), app/utils/authCookie.ts (DEMO_TOKEN_COOKIE), app/assets/css/site.css (classes wrap, section, panel, form, button, form__error, notice, muted, link-button), i18n/locales/en.json and pl.json, vitest.config.ts (Node tests under app/**/*.test.ts), nuxt.config.ts (`/_user` already proxied in dev).
- sm-demo-app: summer.yaml, go.work, go.mod (TestGoModRequiresTags in go_mod_test.go), .gitmodules, boot_test.go (`wantPlugins`, `TestBootAllPlugins`, `routed`), sociallogin_test.go (`socialServer` boot without a database), plugins.gen.go, deploy/env.example, README.md, config/app.yaml (`url: http://127.0.0.1:8097`, `jwt.blacklist_grace: 10`), DEPLOY.md (same-host nginx routing on demo.summercms.io); mount precedent commit `d5a98d9` (`git -C ../sm-demo-app show --stat d5a98d9`), bump precedents `1afd7ff`, `f16ef6c`.
- sm-bm-app: summer.yaml, go.mod (`v0.0.0` requires with `replace => ./plugins/...`), go.work, .gitmodules, sociallogin_test.go (`TestSocialLoginMounted`), realtime_test.go (boot harness), plugins.gen.go; mount precedent commit `117ef8f`, bump precedents `cc4b23f`, `1e09c23`.
- fonoteka.go: plugins/golem15/fonoteka/phase07_coverage_test.go :79-112 (`banned` list), plugins/golem15/fonoteka/classes/hidden_marshal_test.go (`expectedUserModels`), parity/schema_diff_test.go (Go-only table allowances), plugins/golem15/fonoteka/console/dev_seed_test.go and dev_stack_smoke_test.go (table lists); precedent `7fc790d` (a user-plugin model added with the count bumped).
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: a signed-in user registers a security key, the next login lists webauthn, and a WebAuthn assertion against server-side options completes the challenge with {token, user}</name>
<reversibility rating="costly">D-08: the standard base64url WebAuthn request and response shapes become what clients implement; changing them later breaks every client built against them.</reversibility>
<files>../sm-mfa-plugin/go.mod, ../sm-mfa-plugin/go.sum, ../sm-mfa-plugin/routes.go, ../sm-mfa-plugin/config/config.yaml, ../sm-mfa-plugin/classes/config.go, ../sm-mfa-plugin/classes/webauthn.go, ../sm-mfa-plugin/classes/gate.go, ../sm-mfa-plugin/classes/methods.go, ../sm-mfa-plugin/classes/admin.go, ../sm-mfa-plugin/classes/events.go, ../sm-mfa-plugin/controllers/webauthn.go, ../sm-mfa-plugin/models/webauthn_credential.go, ../sm-mfa-plugin/models/registry.go, ../sm-mfa-plugin/updates/202610220006_create_golem15_user_webauthn_credentials.go, ../sm-mfa-plugin/updates/registry.go, ../sm-mfa-plugin/webauthn_authenticator_test.go, ../sm-mfa-plugin/webauthn_smoke_test.go, ../sm-mfa-plugin/README.md</files>
<read_first>19.6-02-SUMMARY.md, ../sm-mfa-plugin/classes/challenge.go, ../sm-mfa-plugin/classes/gate.go, ../sm-mfa-plugin/classes/methods.go, ../sm-mfa-plugin/controllers/challenge.go, ../sm-mfa-plugin/controllers/management.go, ../sm-mfa-plugin/routes.go, ../sm-sociallogin-plugin/classes/codes.go (:171-215 cooler Set and Pull), modules/cooler/cooler.go, go-webauthn v0.18.2 sources in the module cache (webauthn/types.go, registration.go, login.go, credential.go, types_session.go, doc.go; protocol/options.go, authenticator.go, credential.go, assertion.go), 19.6-RESEARCH.md (Q6, Pattern 3, Code Examples 5 and 6, Pitfalls 6, 7, 8, Assumptions A3, A4, A11, A12), /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/classes/twofactor/WebAuthnMethod.php, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/controllers/TwoFactorApiController.php (:146-218, :380-470)</read_first>
<action>Per D-04, D-05, D-06, D-08, D-16 (sixth table), D-19. Re-check the pin first when today is after 2026-11-20 (`go list -m -json github.com/go-webauthn/webauthn@latest`); install exactly `go get github.com/go-webauthn/webauthn@v0.18.2` unless the user approves another version, then `go mod tidy`; plugin code never imports the archived YAML v3 module.
Migration `golem15_user_webauthn_credentials` (id BIGSERIAL PK, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, credential_id TEXT NOT NULL (base64url raw id) with a unique index, public_key TEXT NOT NULL (base64url COSE key), attestation_type VARCHAR(255) NULL, transports JSONB NULL, sign_count BIGINT NOT NULL DEFAULT 0, name VARCHAR(255) NULL, is_enabled BOOLEAN NOT NULL DEFAULT TRUE, last_used_at TIMESTAMPTZ NULL, credential_data TEXT NULL, passwordless BOOLEAN NOT NULL DEFAULT FALSE, created_at, updated_at, index (user_id)) and model `WebAuthnCredential` (CredentialData `json:"-"`).
classes/webauthn.go: config keys rp_id, rp_name, origins in classes/config.go and config/config.yaml with the defaults in Decisions; `WebAuthnFor(ctx, app)` builds a `webauthn.New` per call (RESEARCH Code Example 5: AttestationPreference none, Login and Registration timeouts 60 s enforced) so settings and config changes apply without restart; `UserHandle(id)` 8 bytes big-endian; a user adapter implementing `webauthn.User` (WebAuthnID the handle, name and display name the user's email and name, credentials decoded from the enabled rows' credential_data). `BeginRegistration(ctx, app, cache, user, passwordless bool)`: exclusions of the user's existing credentials, conveyance none, user verification preferred, and for a passkey resident key required, user verification required and the credProps extension; store the SessionData JSON plus the passwordless flag in cooler under `mfa:webauthn:register:<user id>:<challenge>` for 120 s; return `{"options": creation, "challenge": session.Challenge}`. `FinishRegistration(ctx, app, cache, db, user, challenge, attestationJSON, name)`: `Pull` the session (miss or other user → error), `protocol.ParseCredentialCreationResponseBytes`, `CreateCredential`, insert the row (credential_id and public_key base64url, attestation format, transports, sign count, name or "Security Key", credential_data, passwordless per Decisions). `BeginAssertion(ctx, app, db, challengeRow, user)`: allowed credentials from the user's enabled rows, user verification preferred, store `session.Challenge` in the challenge row's code with method webauthn through a guarded UPDATE (uncompleted, unexpired). `FinishAssertion(ctx, app, tx, challengeRow, user, assertionJSON)`: rebuild SessionData from the row (Challenge, RelyingPartyID, UserID handle, AllowedCredentialIDs from the database, UserVerification preferred, Expires the challenge's expires_at), parse with `protocol.ParseCredentialRequestResponseBytes`, `ValidateLogin`, refuse a clone warning, write the updated Credential back (credential_data, sign_count, last_used_at). Library errors are mapped to fixed reasons and never returned to clients or logged with their text.
controllers/webauthn.go: `WebAuthnRegisterOptions` (jwt.auth; 403 `{"error":"WebAuthn is not available."}` unless webauthn is available; `passwordless` honoured only with two_factor_passwordless_login on), `WebAuthnRegister` (fields attestation required object, challenge required string, name nullable string max 255; failures answer 422 `{"error":"Registration failed: <fixed reason>"}` with reasons such as "invalid or expired challenge" and "verification failed"; success upserts and enables the webauthn method row, runs the ensure-recovery-codes rule (D-19), fires WebAuthnRegisteredEvent and answers `{"message":"Security key registered successfully.","credential":{"id","name","created_at"},"status":...}` plus recovery_codes when a set was created), `WebAuthnRemove` (DELETE webauthn/{id} scoped by user id; 404 `{"error":"Credential not found."}`; deleting the last enabled credential disables the method, fires DisabledEvent and runs the last-method cleanup; WebAuthnRemovedEvent; `{"message":"Security key removed.","status":...}`), `WebAuthnOptions` (unauthenticated, PHP validation; FindChallenge else 401 `{"error":"Invalid or expired challenge."}`, missing user 401 `{"error":"Invalid challenge."}`, failure cap 401; answers `{options, challenge}`), `WebAuthnVerify` (validation `challenge_token` size 64 and `assertion` required object; BeginAttempt; a row without method webauthn or code → 401 `{"error":"Invalid or expired challenge. Request WebAuthn options first."}`; FinishAssertion failure → FailedEvent and 401 `{"error":"WebAuthn verification failed.","two_factor_failed":true}`; success → CompleteChallenge, CompleteLogin, VerifiedEvent with method webauthn, trusted_device_token per D-10). Status lists webauthn_credentials `{id, name (default "Security Key"), last_used_at, created_at}` with wire.Time; `EnabledMethods` keeps webauthn only with an enabled credential (PHP); Admin.Reset and HasTwoFactor include the credentials (delete them; a credential counts). Routes: challenge group `g.Post("/webauthn/options", ...)`, `g.Post("/webauthn/verify", ...)`; management group `g.Post("/webauthn/register/options", ...)`, `g.Post("/webauthn/register", ...)`, `g.Delete("/webauthn/{id}", ...)` followed directly by `g.Where("id", "[0-9]+")`.
Test helper webauthn_authenticator_test.go: `virtualAuthenticator` holding an ECDSA P-256 key, a random credential id, a sign counter and flags (UP, UV, BE, BS settable), producing the RegistrationResponseJSON (attestation format none, attested credential data with a zero AAGUID and the COSE EC2 key, clientDataJSON type webauthn.create with the challenge and origin) and the AuthenticationResponseJSON (authenticator data with the RP ID hash, flags and counter, clientDataJSON type webauthn.get, ES256 signature over authData plus SHA-256 of clientDataJSON, userHandle when given), encoding CBOR with go-webauthn's own CBOR helper package (or fxamacker/cbor/v2, already in the graph through go-webauthn, if the helper is not exported). Smoke tests: `TestWebAuthnCeremonySmoke` (no database, -short: WebAuthnFor with rp_id localhost and origin http://localhost:3000; registration options carry attestation none and the challenge; FinishRegistration's library path accepts the virtual authenticator's response; an assertion with a counter that went backwards is refused as a clone; an assertion with BE changed is refused). `TestWebAuthnSmoke` (Postgres, rp_id localhost, origins http://localhost:3000): register options, register with the virtual authenticator (name "Laptop key") answers the PHP message with credential and recovery_codes; status lists the credential; login answers available_methods containing webauthn; webauthn/verify before options answers the "Request WebAuthn options first." 401; options then verify answers a token and the stored sign_count advanced; a second register with the same challenge fails (state consumed); DELETE webauthn/{id} of the only credential disables the method and the next login answers a token for a user without other methods. README (same commit): WebAuthn section (shapes per D-08, RP ID and origins, IP hosts refused, Redis needed for multi-instance ceremonies, PHP credentials not importable as-is). Commit: `feat: WebAuthn security keys as a second factor`.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short -run '^TestWebAuthnCeremonySmoke$' -v . 2&gt;&amp;1 | grep -q -- '^--- PASS: TestWebAuthnCeremonySmoke'</automated>
<fails_when>Non-zero exit: vet or a short test is red, or no "--- PASS: TestWebAuthnCeremonySmoke" line (the virtual authenticator's responses are refused or the clone and BE checks do not hold).</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -run '^(TestWebAuthnSmoke|TestTracerSmoke|TestChallengeSmoke|TestTrustedSettingsSmoke)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 4</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 4: the WebAuthn path failed, an earlier smoke regressed, or a test skipped because Docker was unreachable (rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'github.com/go-webauthn/webauthn v0.18.2' ../sm-mfa-plugin/go.mod` prints 1 (or the version the user approved, recorded in the SUMMARY).
- `grep -c 'credential_data' ../sm-mfa-plugin/updates/202610220006_create_golem15_user_webauthn_credentials.go` prints at least 1 and `grep -c 'passwordless' ../sm-mfa-plugin/updates/202610220006_create_golem15_user_webauthn_credentials.go` prints at least 1.
- `grep -c 'Pull(' ../sm-mfa-plugin/classes/webauthn.go` prints at least 1 and `grep -c 'PreferNoAttestation' ../sm-mfa-plugin/classes/webauthn.go` prints at least 1.
- `grep -A1 '"/webauthn/{id}"' ../sm-mfa-plugin/routes.go | grep -c 'Where("id", "\[0-9\]+")'` prints 1.
- `grep -c '"WebAuthn verification failed."' ../sm-mfa-plugin/controllers/webauthn.go` prints at least 1 and `grep -c 'Request WebAuthn options first.' ../sm-mfa-plugin/controllers/webauthn.go` prints 1.
- `D=$(go -C ../sm-mfa-plugin list -deps .) && test "$(printf '%s\n' "$D" | grep -c '^gopkg.in/yaml')" -eq 0` exits 0.
</acceptance_criteria>
<done>A user can add a security key and sign in with it as their second factor, with all ceremony state held on the server.</done>
</task>
<task type="auto">
<name>Task 2: A user who registered a passkey signs in with it alone when the site allows passwordless login, and social sign-in asks for the second factor when the site turns that on</name>
<reversibility rating="costly">D-14: once users register passkeys and sign in without a password, removing the passwordless routes strands them.</reversibility>
<files>../sm-mfa-plugin/classes/passwordless.go, ../sm-mfa-plugin/controllers/passwordless.go, ../sm-mfa-plugin/routes.go, ../sm-mfa-plugin/lang/en/lang.yaml, ../sm-mfa-plugin/lang/pl/lang.yaml, ../sm-mfa-plugin/passwordless_smoke_test.go, ../sm-mfa-plugin/README.md, ../sm-sociallogin-plugin/classes/flow.go, ../sm-sociallogin-plugin/classes/codes.go, ../sm-sociallogin-plugin/controllers/complete.go, ../sm-sociallogin-plugin/controllers/register_complete.go, ../sm-sociallogin-plugin/lang/en/lang.yaml, ../sm-sociallogin-plugin/lang/pl/lang.yaml, ../sm-sociallogin-plugin/social_gate_smoke_test.go, ../sm-sociallogin-plugin/README.md</files>
<read_first>../sm-mfa-plugin/classes/webauthn.go and controllers/webauthn.go (Task 1), ../sm-mfa-plugin/webauthn_authenticator_test.go, ../fonoteka.go/plugins/golem15/user/classes/throttle.go (RejectIfThrottled :67), ../fonoteka.go/plugins/golem15/user/controllers/login_gates.go, ../sm-sociallogin-plugin/classes/flow.go (signIn :311-324, linkIdentity, Outcome), ../sm-sociallogin-plugin/classes/codes.go, ../sm-sociallogin-plugin/controllers/complete.go, ../sm-sociallogin-plugin/controllers/register_complete.go, ../sm-sociallogin-plugin/controllers/callback.go, ../sm-sociallogin-plugin/classes/messages.go, ../sm-sociallogin-plugin/tracer_smoke_test.go, ../sm-sociallogin-plugin/harness_test.go, ../sm-sociallogin-plugin/README.md, /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/components/TwoFactor.php (:278, :293), 19.6-RESEARCH.md (Q5 sociallogin, Q6 passwordless, Code Example 6, Security Domain, Assumption A7)</read_first>
<action>D-14 in sm-mfa-plugin. classes/passwordless.go: `BeginPasswordless(ctx, app, cache)` with `BeginDiscoverableLogin(webauthn.WithUserVerification(protocol.VerificationRequired))`, session JSON in cooler under `mfa:webauthn:passwordless:-:<challenge>` for 120 s, returning `{options, challenge}`; `FinishPasswordless(ctx, app, cache, db, challenge, assertionJSON, ip)` per RESEARCH Code Example 6: Pull the session (miss → failure); `ValidatePasskeyLogin` with a handler that loads the credential by the base64url raw id and refuses it unless it exists, is enabled, has passwordless true and its user's handle equals the returned userHandle; refuse a clone warning; write the credential back; then the user (soft-deleted ones included through Unscoped) must be activated and pass `userclasses.RejectIfThrottled` (ban and suspension); any refusal is one generic failure. controllers/passwordless.go: `PasswordlessOptions` and `PasswordlessVerify` (fields challenge required string, assertion required object); both answer 403 `{"error":"Passwordless login is not available."}` while `two_factor_passwordless_login` is off (read per request); a failure answers 401 `{"error":"WebAuthn verification failed."}`; success runs `usercontrollers.FinishLogin` (restores a deactivated account, as a full password-plus-second-factor login would), `IssueToken`, `APIArray`, `FireLoginEvent` with `userclasses.LoginSourcePasskey`, and answers `{token, user}`; no gate, no second challenge (D-14). Routes in the challenge group (2fa-verify bucket): `g.Post("/webauthn/passwordless/options", ...)`, `g.Post("/webauthn/passwordless/verify", ...)`. README (same commit): Passkeys section (setting, passkey registration with passwordless true, the UV and resident-key rules, activation, ban and suspension checks, no second challenge, the security review in plan 04). Smoke test passwordless_smoke_test.go `TestPasswordlessSmoke` (Postgres, rp_id localhost) with subtests: `setting_off` (both routes 403); `passkey_signs_in` (setting on, register with passwordless true, UV and credProps rk true, then options and verify with the userHandle answer a token for that user and fire one login event with Source passkey); `second_factor_key_refused` (a credential registered without passwordless answers 401); `uv_missing_refused` (an assertion without the UV flag answers 401); `banned_refused` (a banned user's passkey answers 401); `state_once` (verify reusing a consumed challenge answers 401). Commit in sm-mfa-plugin: `feat: passwordless passkey login`.
D-09 in sm-sociallogin-plugin. classes/flow.go `signIn`: before FinishLogin call `usercontrollers.CheckLoginGates(ctx, app, userclasses.LoginAttempt{User: user, Source: userclasses.LoginSourceSocial, Request: r})`; an error returns the error (the callback's existing error path; no token); a result with status 200 (or 0) returns `Outcome{Kind: OutcomeLogin, Challenge: res.Body, Action: action, ReturnTo: returnTo}` without calling FinishLogin or IssueToken; any other status returns `FlowError{Key: "two_factor_unavailable"}`. `linkIdentity` stays ungated. classes/codes.go: `CompletionPayload` gains `Challenge map[string]any` with JSON name challenge and omitempty, and the callback stores the outcome's Challenge in it. controllers/complete.go: when the pulled payload has a Challenge, answer 200 with exactly that body (no token cookie handling); register_complete.go likewise for an outcome with a Challenge. Lang en and pl `oauth.two_factor_unavailable` ("Two-factor verification is temporarily unavailable. Try again later." and Polish). go.mod keeps requiring sm-user-plugin v0.1.5 with the go.work replace until Task 5. README (same commit): a "Two-factor sign-in" section: with a two-factor plugin that enables its social-login setting, oauth-complete and oauth-register-complete answer the two_factor_required body instead of a token, the SPA callback must handle it before the setting is turned on, linking is never gated, trusted devices do not apply (no login body). Smoke test social_gate_smoke_test.go `TestSocialGateSmoke` (Postgres, a fake gate added to the published LoginGates, no dependency on sm-mfa-plugin) with subtests: `no_gate_unchanged` (the tracer's sign-in answers token, user, action, return_to); `challenge_body` (a gate returning Status 200 with a two_factor_required body: oauth-complete answers exactly that body, no token key, and a soft-deleted identity owner stays soft-deleted); `refusal_redirects` (Status 401: the callback redirects with ?error= carrying the two_factor_unavailable message); `gate_error_fails_closed` (no completion code with a token is issued); `link_not_gated` (action=link with a link ticket calls the gate zero times). Commit in sm-sociallogin-plugin: `feat: social sign-in through the login gate`.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./... &amp;&amp; go -C ../sm-sociallogin-plugin vet ./... &amp;&amp; go -C ../sm-sociallogin-plugin test -count=1 -short ./...</automated>
<fails_when>Non-zero exit: vet or a short test of either plugin is red.</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -run '^(TestPasswordlessSmoke|TestWebAuthnSmoke)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 2: a passkey rule does not hold or a test skipped because Docker was unreachable (rerun through sudo -u golem, else record NOT RUN).</fails_when>
<automated>go -C ../sm-sociallogin-plugin test -count=1 -run '^(TestSocialGateSmoke|TestTracerSmoke)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 2: the social gate behaves wrongly, the v0.1.2 tracer regressed, or a test skipped (Docker unreachable: record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'ValidatePasskeyLogin' ../sm-mfa-plugin/classes/passwordless.go` prints at least 1 and `grep -c 'VerificationRequired' ../sm-mfa-plugin/classes/passwordless.go` prints at least 1.
- `grep -c '"/webauthn/passwordless/options"' ../sm-mfa-plugin/routes.go` prints 1 and `grep -c '"/webauthn/passwordless/verify"' ../sm-mfa-plugin/routes.go` prints 1.
- `grep -c 'Passwordless login is not available.' ../sm-mfa-plugin/controllers/passwordless.go` prints at least 1.
- `grep -c 'CheckLoginGates(' ../sm-sociallogin-plugin/classes/flow.go` prints 1 and `grep -c 'LoginSourceSocial' ../sm-sociallogin-plugin/classes/flow.go` prints 1.
- `grep -c 'json:"challenge,omitempty"' ../sm-sociallogin-plugin/classes/codes.go` prints 1.
- `grep -c 'two_factor_unavailable' ../sm-sociallogin-plugin/lang/en/lang.yaml` prints 1 and the same count for lang/pl/lang.yaml is 1.
- `grep -c 'two_factor_required' ../sm-sociallogin-plugin/README.md` prints at least 1.
</acceptance_criteria>
<done>A user with a passkey signs in without a password where the site allows it, and sites that want it can require the second factor after a social sign-in too.</done>
</task>
<task type="auto">
<name>Task 3: The demo site lets a signed-in user turn on an authenticator app with a QR code, keep their recovery codes, turn it off, and answer the login challenge with a TOTP or recovery code</name>
<files>../sm-demo-app/vue-demo-app/app/lib/twoFactor.ts, ../sm-demo-app/vue-demo-app/app/lib/twoFactor.test.ts, ../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts, ../sm-demo-app/vue-demo-app/app/composables/useTwoFactor.ts, ../sm-demo-app/vue-demo-app/app/pages/login.vue, ../sm-demo-app/vue-demo-app/app/pages/account/security.vue, ../sm-demo-app/vue-demo-app/app/components/SiteHeader.vue, ../sm-demo-app/vue-demo-app/i18n/locales/en.json, ../sm-demo-app/vue-demo-app/i18n/locales/pl.json</files>
<read_first>../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts, ../sm-demo-app/vue-demo-app/app/pages/login.vue, ../sm-demo-app/vue-demo-app/app/pages/register.vue, ../sm-demo-app/vue-demo-app/app/pages/subscriptions.vue, ../sm-demo-app/vue-demo-app/app/components/SiteHeader.vue, ../sm-demo-app/vue-demo-app/app/utils/authCookie.ts, ../sm-demo-app/vue-demo-app/app/assets/css/site.css, ../sm-demo-app/vue-demo-app/i18n/locales/en.json, ../sm-demo-app/vue-demo-app/i18n/locales/pl.json, ../sm-demo-app/vue-demo-app/vitest.config.ts, ../sm-demo-app/vue-demo-app/app/lib/forms.ts, ../sm-demo-app/vue-demo-app/app/utils/journalLocale.test.ts (test style), /media/nvme/dev/jz/wavepath.org/vue-wavepath-app/src/types/auth.ts (:157-162), 19.6-02-SUMMARY.md (management response bodies, D-19 recovery_codes), 19.6-PATTERNS.md (VDEMO section)</read_first>
<action>Per D-20, in the submodule checkout ../sm-demo-app/vue-demo-app (check `git -C ../sm-demo-app/vue-demo-app status --porcelain` is empty and its branch is master tracking origin first; stop and report otherwise). Never touch ../vue-demo-app.
app/lib/twoFactor.ts (pure, unit-tested): types `TwoFactorChallenge` ({two_factor_required: true, challenge_token, available_methods, expires_at}), `TwoFactorStatus` (the PHP status keys), `TotpSetup` ({secret, qr_code_svg, qr_code_url}); `isTwoFactorChallenge(value)` (true only for an object with two_factor_required === true and a 64-character string challenge_token); `normalizeRecoveryCode(s)` (uppercase, spaces removed, as the server does); `isTotpCode(s)` (exactly six digits after trimming); `challengeExpired(expiresAt, now)` (true for an unparsable or past expires_at); `svgDataUrl(svg)` (`data:image/svg+xml;base64,` plus base64 of the UTF-8 bytes, so the server's SVG is shown through an img and never injected as markup). app/lib/twoFactor.test.ts covers each with positive and negative cases (a token answer is not a challenge; a 63-character token is not; " abcde-23456 " normalizes to "ABCDE-23456"; "12345" and "1234567" are not TOTP codes; the data URL decodes back to the input).
useDemoAuth.ts: `login` returns `{ challenge: TwoFactorChallenge }` when the answer is a challenge and `{}` after storing a token as today (the caller decides); new `verifyChallenge(challengeToken, code)` posts `{challenge_token, method: 'totp', code}` to `/_user/api/v1/2fa/verify` and `recoverChallenge(challengeToken, code)` posts to `/_user/api/v1/2fa/recovery`, both storing the returned token. useTwoFactor.ts: `useTwoFactor()` with `status()`, `setupTotp()`, `confirmTotp(secret, code)` (returns the status and the recovery_codes when present) and `disableTotp()`, each `$fetch` with `Authorization: Bearer <token>` from the DEMO_TOKEN_COOKIE cookie. login.vue: after a challenge answer, show a second form in the same panel: a code input (inputmode numeric, autocomplete one-time-code, maxlength 6) with a submit, a link-button switching to a recovery-code input, a "start over" link-button back to the password form; an expired challenge (challengeExpired) or a 401 shows the server's error through authErrors and offers start over; success navigates to `next` as today. New pages/account/security.vue: logged-out notice like subscriptions.vue; when TOTP is off, "Set up an authenticator app" calls setupTotp and shows the QR through `<img :src="svgDataUrl(setup.qr_code_svg)">` with width and height 200 and alt text, the secret as copyable text for manual entry and a confirm form; after confirm, when recovery_codes came back, show them once in a list with a note to store them and a "Saved them" button that clears them from memory; when TOTP is on, the remaining recovery-code count and a "Turn off" button with a confirm(). SiteHeader.vue: a NuxtLink to /account/security beside "My presentations" when logged in. i18n en and pl: keys `auth.twoFactor.*` (title, prompt, code label, verify, useRecovery, useApp, recoveryLabel, startOver, expired, failed) and `security.*` (title, lead, setup, scan, manual, confirm, enabled, disabled, turnOff, turnOffConfirm, recoveryTitle, recoveryNote, saved, remaining, loginFirst) with natural Polish. Styling uses only the existing classes (panel, form, button, link-button, form__error, notice, muted).
Commit in the vue-demo-app repository (local only until Task 5): `feat: two-factor setup and the login challenge`.</action>
<verify>
<automated>pnpm --dir ../sm-demo-app/vue-demo-app test &amp;&amp; pnpm --dir ../sm-demo-app/vue-demo-app exec vitest run --reporter=verbose app/lib/twoFactor.test.ts 2&gt;&amp;1 | grep -q 'twoFactor.test.ts'</automated>
<fails_when>Non-zero exit of the vitest suite, or no line naming twoFactor.test.ts in the verbose run (the new tests are missing or were not collected).</fails_when>
<automated>pnpm --dir ../sm-demo-app/vue-demo-app build</automated>
<fails_when>Non-zero exit: the Nuxt build fails (type or template errors in the new page, composables or header).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'v-html' ../sm-demo-app/vue-demo-app/app/pages/account/security.vue` prints 0 and `grep -c 'svgDataUrl' ../sm-demo-app/vue-demo-app/app/pages/account/security.vue` prints at least 1.
- `grep -c '2fa/verify' ../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts` prints 1 and `grep -c '2fa/recovery' ../sm-demo-app/vue-demo-app/app/composables/useDemoAuth.ts` prints 1.
- `grep -c 'totp/setup' ../sm-demo-app/vue-demo-app/app/composables/useTwoFactor.ts` prints 1 and `grep -c 'recovery_codes' ../sm-demo-app/vue-demo-app/app/composables/useTwoFactor.ts` prints at least 1.
- `grep -c '/account/security' ../sm-demo-app/vue-demo-app/app/components/SiteHeader.vue` prints at least 1.
- `grep -c '"twoFactor"' ../sm-demo-app/vue-demo-app/i18n/locales/en.json` prints 1 and the same count for pl.json is 1.
- `R=$(git -C ../sm-demo-app/vue-demo-app remote get-url origin) && test "$R" = "git@git.golem15.com:golem15/vue-demo-app.git"` exits 0 and `S=$(git -C ../vue-demo-app status --porcelain) && test -z "$S"` exits 0 (the stale sibling stays untouched).
</acceptance_criteria>
<done>A visitor on the demo can protect their account with an authenticator app, keep their recovery codes, and pass the extra login step.</done>
</task>
<task type="checkpoint:decision" gate="blocking">
<name>Task 4: Go-ahead for the phase's remote operations in D-17 order (sm-user-plugin push and tag, new sm-mfa-plugin repository and tag, sm-sociallogin-plugin push and tag, vue-demo-app push, then sm-demo-app and sm-bm-app pushes after their commits)</name>
<read_first>19.6-CONTEXT.md (D-17), 19.6-01-SUMMARY.md, 19.6-02-SUMMARY.md, CLAUDE.md (Commits and concurrency), .planning/notes/core-plugins-own-repos.md</read_first>
<acceptance_criteria>
- The SUMMARY draft records the reply verbatim under a "Task 4 reply" heading, with the tag numbers it names.
- Before the reply, `R=$(git -C ../sm-mfa-plugin remote) && test -z "$R"` exits 0 and `T=$(git -C ../fonoteka.go/plugins/golem15/user tag --points-at HEAD) && test -z "$T"` exits 0.
</acceptance_criteria>
<decision>Which remote operations may this phase perform, and with which tags?</decision>
<context>Tasks 1-3 and plans 01-02 are committed locally and green. Task 6 mounts sm-mfa-plugin as a git submodule in sm-demo-app and sm-bm-app and bumps sm-user-plugin and sm-sociallogin-plugin pointers, which must reference pushed commits (CLAUDE.md: plugin pushed first, pointer bump in the application as a separate commit). sm-demo-app's TestGoModRequiresTags requires every mounted plugin at a vX.Y.Z tag, so the demo cannot mount mfa without tags; sm-bm-app pins plugins as v0.0.0 with replaces and needs only pushed commits. Proposed tags: sm-user-plugin the next patch after its newest tag on origin (v0.1.6 unless Phase 19.8 tagged one meanwhile; it carries the login gates, events, reset slot and the device table migration, which become one-way for applications that run migrate), sm-sociallogin-plugin v0.1.3, sm-mfa-plugin v0.1.0 (new private repository created with `tea repos create --owner golem15 --name sm-mfa-plugin`). vue-demo-app is pushed without a tag. No framework tag and no changelog post are needed (no framework change). fonoteka.go gets a local commit only, as always. Nothing is deployed in any option.</context>
<options>
<option id="release">
<name>release (optionally naming tags, for example "release user v0.1.6 sociallogin v0.1.3 mfa v0.1.0")</name>
<pros>D-17 exactly: tagged, reproducible pins; the demo and bm mount mfa in this phase; the device flow reaches both applications.</pros>
<cons>Publishes the device-session migration and the new tables; later changes need migrations.</cons>
</option>
<option id="push-only">
<name>push-only</name>
<pros>Repositories exist; sm-bm-app (v0.0.0 replaces) mounts mfa; fonoteka.go and sm-bm-app move to the new user plugin.</pros>
<cons>No tags: sm-demo-app cannot mount mfa or bump the user plugin (TestGoModRequiresTags), so its part of D-17 and the D-20 UI pointer bump wait for a later release; recorded as a deviation with a todo.</cons>
</option>
<option id="hold">
<name>hold</name>
<pros>Nothing leaves this machine.</pros>
<cons>Task 5 and the remote half of Task 6 do not run; only fonoteka.go's local pointer bump can happen; D-17 remains open and the phase pauses after this plan.</cons>
</option>
</options>
<resume-signal>Reply "release" (optionally with tags), "push-only" or "hold". release and push-only also authorise pushing sm-demo-app and sm-bm-app after Task 6's commits, each only when its unpushed commits are this phase's (or ones the reply names); nothing is ever force-pushed or deployed. Say whether plan 04 may publish patch tags for post-release fixes under this same reply or must ask again.</resume-signal>
</task>
<task type="auto">
<name>Task 5: Under the Task 4 reply, publish in D-17 order: sm-user-plugin, then sm-mfa-plugin and sm-sociallogin-plugin pinned to it, then vue-demo-app</name>
<precondition>19.6-03-SUMMARY.md (draft) records the Task 4 reply "release" or "push-only"; on "hold" this task records "remote operations held" and does nothing else.</precondition>
<reversibility rating="one-way">Pushed tags and the device-session and 2FA migrations become what applications pin and run; undoing them needs new releases and migrations (gated by Task 4).</reversibility>
<files>../sm-mfa-plugin/go.mod, ../sm-mfa-plugin/go.sum, ../sm-mfa-plugin/README.md, ../sm-sociallogin-plugin/go.mod, ../sm-sociallogin-plugin/go.sum</files>
<read_first>19.6-03-SUMMARY.md draft (Task 4 reply), 19.6-01-SUMMARY.md and 19.6-02-SUMMARY.md (this phase's commits per repository), CLAUDE.md (Commits and concurrency), .planning/STATE.md decisions for Phases 19.1 and 19.5 (tea repos create precedent), ../sm-sociallogin-plugin/go.mod</read_first>
<action>Per D-17 and the recorded reply. Before every push: `git fetch origin`, `git status --porcelain` shows no change to this phase's paths, and `git log --oneline origin/master..master` lists only this phase's commits from the SUMMARYs (or ones the reply names); if anything else appears, or origin moved ahead, stop and report (no rebase without the user). Never force-push. Never deploy.
On "release": (1) sm-user-plugin (../fonoteka.go/plugins/golem15/user): `go -C ../fonoteka.go vet ./plugins/golem15/user/...` and the short suite green; `git push origin master`; annotated tag with the reply's version (one paragraph: login gates, login and password-changed events, reset-2FA slot, device authorization); push the tag. (2) sm-mfa-plugin: require that sm-user-plugin tag and the framework version sm-sociallogin-plugin requires (v0.3.1; a newer tag on origin only if the code needs one, checked with `git -C ../summercms.go ls-remote --tags origin`), `go mod tidy`, vet and short tests green, README Dependencies names both versions; commit `chore: require sm-user-plugin <tag>`; `tea repos create --owner golem15 --name sm-mfa-plugin` (private unless the reply says otherwise), `git remote add origin git@git.golem15.com:golem15/sm-mfa-plugin.git`, `git push -u origin master`, annotated tag v0.1.0 (or the reply's), push it. (3) sm-sociallogin-plugin: require the same sm-user-plugin tag, `go mod tidy`, vet and short tests green; commit `chore: require sm-user-plugin <tag>`; push master; annotated tag (v0.1.3 or the reply's); push it. (4) vue-demo-app (../sm-demo-app/vue-demo-app): push master (no tag).
On "push-only": the same pushes without tags; sm-mfa-plugin and sm-sociallogin-plugin keep their current requires (recorded deviation) and the repository is created and pushed without a tag.
Record in the SUMMARY: each pushed commit range, each tag, the new repository URL.</action>
<verify>
<automated>go -C ../sm-mfa-plugin vet ./... &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short ./... &amp;&amp; git -C ../sm-mfa-plugin ls-remote --exit-code origin refs/heads/master &amp;&amp; git -C ../fonoteka.go/plugins/golem15/user ls-remote --exit-code origin refs/heads/master</automated>
<fails_when>Non-zero exit: the plugin no longer builds or passes after the require bump, or ls-remote exits 2 or 128 because a pushed branch does not exist (repository not created or not pushed). On "hold" the command is not run and the SUMMARY says so.</fails_when>
</verify>
<acceptance_criteria>
- On release: `git -C ../fonoteka.go/plugins/golem15/user ls-remote --exit-code --tags origin <user tag>` exits 0, `git -C ../sm-mfa-plugin ls-remote --exit-code --tags origin v0.1.0` exits 0, `git -C ../sm-sociallogin-plugin ls-remote --exit-code --tags origin <sociallogin tag>` exits 0, and `grep -c 'git.golem15.com/golem15/sm-user-plugin <user tag>' ../sm-mfa-plugin/go.mod` prints 1.
- On release and push-only: `L=$(git -C ../fonoteka.go/plugins/golem15/user log --oneline origin/master..master) && test -z "$L"` exits 0 and `L=$(git -C ../sm-demo-app/vue-demo-app log --oneline origin/master..master) && test -z "$L"` exits 0.
</acceptance_criteria>
<done>The user plugin, the new two-factor plugin, the social-login plugin and the demo frontend are on git.golem15.com in D-17 order, so applications can pin them.</done>
</task>
<task type="auto">
<name>Task 6: The demo and bm applications mount two-factor sign-in and get the device flow, and fonoteka.go moves to the new user plugin with its guard tests updated</name>
<precondition>19.6-03-SUMMARY.md (draft) records Task 5's pushes for "release" or "push-only"; on "hold" only part (3) below runs.</precondition>
<files>../sm-demo-app/.gitmodules, ../sm-demo-app/plugins/golem15/mfa, ../sm-demo-app/plugins/golem15/user, ../sm-demo-app/plugins/golem15/sociallogin, ../sm-demo-app/vue-demo-app, ../sm-demo-app/go.mod, ../sm-demo-app/go.sum, ../sm-demo-app/go.work, ../sm-demo-app/summer.yaml, ../sm-demo-app/plugins.gen.go, ../sm-demo-app/boot_test.go, ../sm-demo-app/mfa_test.go, ../sm-demo-app/config/golem15.mfa.yaml, ../sm-demo-app/deploy/env.example, ../sm-demo-app/README.md, ../sm-bm-app/.gitmodules, ../sm-bm-app/plugins/golem15/mfa, ../sm-bm-app/plugins/golem15/user, ../sm-bm-app/plugins/golem15/sociallogin, ../sm-bm-app/go.mod, ../sm-bm-app/go.sum, ../sm-bm-app/go.work, ../sm-bm-app/summer.yaml, ../sm-bm-app/plugins.gen.go, ../sm-bm-app/mfa_test.go, ../sm-bm-app/README.md, ../fonoteka.go/plugins/golem15/user, ../fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go, ../fonoteka.go/plugins/golem15/fonoteka/classes/hidden_marshal_test.go</files>
<read_first>19.6-03-SUMMARY.md draft (reply, tags), `git -C ../sm-demo-app show --stat d5a98d9` and `git -C ../sm-bm-app show --stat 117ef8f` (mount precedents), ../sm-demo-app/summer.yaml, ../sm-demo-app/go.work, ../sm-demo-app/go.mod, ../sm-demo-app/go_mod_test.go, ../sm-demo-app/.gitmodules, ../sm-demo-app/boot_test.go, ../sm-demo-app/sociallogin_test.go, ../sm-demo-app/deploy/env.example, ../sm-demo-app/README.md, ../sm-demo-app/DEPLOY.md, ../sm-bm-app/summer.yaml, ../sm-bm-app/go.mod, ../sm-bm-app/go.work, ../sm-bm-app/sociallogin_test.go, ../sm-bm-app/README.md, ../fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go, ../fonoteka.go/plugins/golem15/fonoteka/classes/hidden_marshal_test.go, `git -C ../fonoteka.go show --stat 7fc790d`, ../sm-mfa-plugin/README.md (config keys)</read_first>
<action>Per D-17, each application commit separate, plugins already pushed (Task 5). Check `git status --porcelain` of each application before editing; stop and report when another session's edits touch a file below.
(1) sm-demo-app (release only; on push-only record the deviation and skip to (2)): commit A `chore: bump sm-user-plugin to <tag>` (submodule checkout of the tag, go.mod require, go.sum through `go mod tidy`); commit B `chore: bump sm-sociallogin-plugin to <tag>` likewise; commit C `feat: mount golem15.mfa`, as d5a98d9 did for sociallogin: `git -C ../sm-demo-app submodule add -b master git@git.golem15.com:golem15/sm-mfa-plugin.git plugins/golem15/mfa` (or ssu) at tag v0.1.0, go.work `use ./plugins/golem15/mfa`, summer.yaml entry `- id: golem15.mfa` / `module: git.golem15.com/golem15/sm-mfa-plugin` after golem15.sociallogin and before golem15.demo, go.mod require v0.1.0 and tidy, `summer build` (installed from ../summercms.go with `go install ./cmd/summer`) regenerating plugins.gen.go (keep only that change; report anything else it rewrites), boot_test.go `wantPlugins` gains golem15.mfa in summer.yaml order and the routed prefixes gain `/_user/api/v1/2fa/` and `/_user/api/v1/devices`, config/golem15.mfa.yaml with two_factor_mode optional, two_factor_available_methods [totp, email], the social-login toggle off and a comment that the deployed demo serves SPA and API on one host so the WebAuthn RP defaults to the app.url host while a local 127.0.0.1 run cannot use WebAuthn (Pitfall 7), deploy/env.example with commented SUMMER_GOLEM15__MFA__ lines for the mode and the WebAuthn RP override, README plugin row; new mfa_test.go `TestMFAMounted` booting the committed config without a database (as socialServer does) and asserting that POST /_user/api/v1/2fa/verify, POST /_user/api/v1/2fa/webauthn/passwordless/options, GET /_user/api/v1/2fa/status, POST /_user/api/v1/devices/initiate, GET /_user/api/v1/devices/status/{token} and DELETE /_user/api/v1/devices/{id} resolve; commit D `chore: bump vue-demo-app to the two-factor UI` (pointer only). Push sm-demo-app per the reply after the pre-push check.
(2) sm-bm-app (release and push-only): commit A `chore: bump sm-user-plugin to <tag or commit>` (pointer, plus go.mod or go.sum only if they change); commit B `chore: bump sm-sociallogin-plugin to <tag or commit>`; commit C `feat: mount golem15.mfa` as 117ef8f did: submodule at plugins/golem15/mfa, go.mod `require git.golem15.com/golem15/sm-mfa-plugin v0.0.0` with `replace git.golem15.com/golem15/sm-mfa-plugin => ./plugins/golem15/mfa`, go.work use, summer.yaml entry after golem15.sociallogin, regenerated plugins.gen.go, README row (API only; the frontend has no 2FA screens, so users cannot enable 2FA there yet and logins are unchanged), mfa_test.go `TestMFAMounted` (boot without a database like TestSocialLoginMounted, the same route assertions). Push sm-bm-app per the reply.
(3) fonoteka.go (always, local commit only, never pushed): one commit `chore: bump sm-user-plugin to <tag or commit>` staging only `plugins/golem15/user` (already at the pushed commit, or the local one on hold) and the test updates the full suite needs: phase07_coverage_test.go removes the device-routes fragment from `banned` and keeps the 2fa fragment (fonoteka still must not resolve the two-factor routes); hidden_marshal_test.go raises `expectedUserModels` by one with a comment naming DeviceAuthSession (7fc790d precedent); any other fonoteka test that inventories user-plugin tables, routes or permissions (parity/schema_diff_test.go Go-only allowances, console/dev_seed_test.go, dev_stack_smoke_test.go) gets the matching additive entry with a comment, only where the full suite shows it is needed. fonoteka.go does not mount golem15.mfa.</action>
<verify>
<automated>go -C ../fonoteka.go vet ./... &amp;&amp; go -C ../fonoteka.go test -count=1 -short ./... &amp;&amp; go -C ../fonoteka.go build ./...</automated>
<fails_when>Non-zero exit: fonoteka.go no longer builds, or a short test is red at the bumped user-plugin pointer (for example the guard test still bans /devices or the model count is stale).</fails_when>
<automated>go -C ../sm-demo-app build ./... &amp;&amp; go -C ../sm-demo-app vet . &amp;&amp; go -C ../sm-demo-app test -count=1 -short -run '^(TestMFAMounted|TestBootAllPlugins|TestGoModRequiresTags)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>Non-zero exit, or the count of top-level "--- PASS: Test" lines is not 3: the demo does not build, a route does not resolve, a plugin is missing from wantPlugins, or a require is not a tag. On push-only and hold this command is not run and the SUMMARY says so.</fails_when>
<automated>go -C ../sm-bm-app build ./... &amp;&amp; go -C ../sm-bm-app vet . &amp;&amp; go -C ../sm-bm-app test -count=1 -short . &amp;&amp; go -C ../sm-bm-app test -count=1 -short -run '^TestMFAMounted$' -v . 2&gt;&amp;1 | grep -q -- '^--- PASS: TestMFAMounted'</automated>
<fails_when>Non-zero exit, or no "--- PASS: TestMFAMounted" line: bm does not build or the mfa routes do not resolve. On hold this command is not run and the SUMMARY says so.</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '"/devices"' ../fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go` prints 0 and `grep -c '"2fa"' ../fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go` prints 1.
- `grep -c 'golem15.mfa' ../fonoteka.go/summer.yaml` prints 0 and `M=$(go -C ../fonoteka.go list -m all) && test "$(printf '%s\n' "$M" | grep -c 'go-webauthn')" -eq 0` exits 0 (D-01).
- On release: `grep -c 'id: golem15.mfa' ../sm-demo-app/summer.yaml` prints 1, `grep -c 'git.golem15.com/golem15/sm-mfa-plugin v0.1.0' ../sm-demo-app/go.mod` prints 1, and `grep -c 'golem15.mfa' ../sm-demo-app/plugins.gen.go` prints at least 1.
- On release and push-only: `grep -c 'id: golem15.mfa' ../sm-bm-app/summer.yaml` prints 1 and `L=$(git -C ../sm-bm-app log --oneline origin/master..master) && test -z "$L"` exits 0 after the push.
- `L=$(git -C ../fonoteka.go log -1 --format=%s -- plugins/golem15/user) && test "${L#chore: bump sm-user-plugin}" != "$L"` exits 0 (the pointer moved in the bump commit).
</acceptance_criteria>
<done>Both applications offer two-factor sign-in and device authorization, the demo shows the screens for it, and fonoteka.go builds and passes at the new user plugin without mounting two-factor sign-in.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| browser authenticator to the WebAuthn routes | attestations and assertions arrive from the client and must be bound to server-side ceremony state |
| frontend origin to the RP | the RP ID and allowed origins decide which pages may run ceremonies |
| anonymous client to the passwordless routes | a passkey alone yields a token |
| social provider callback to signIn | a social identity must not bypass an enabled second factor |
| demo frontend to the API | server-generated SVG rendered in the browser |
| this machine to git.golem15.com | tags, pushes and a new repository |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6-26 | Spoofing | WebAuthn challenge supplied by the client (Pitfall 6) | high | mitigate | expected challenges held server-side (challenge row code; cooler Pull for registration and passkeys, single use, 120 s); a client challenge is only a lookup key; TestWebAuthnSmoke state reuse |
| T-19.6-27 | Elevation of privilege | passkey login with a second-factor-only credential (D-14) | high | mitigate | passwordless column set only for requested, UV-verified, credProps-rk credentials; ValidatePasskeyLogin with UV required; handler checks enabled, passwordless and the user handle; setting off by default; TestPasswordlessSmoke; explicit D-14 review in plan 04 |
| T-19.6-28 | Spoofing | cloned authenticator | medium | mitigate | credential written back after every assertion with sign count and flags; a clone warning refuses the login; BE changes refused by the library (Pitfall 8); TestWebAuthnCeremonySmoke |
| T-19.6-29 | Spoofing | RP ID or origin misconfiguration (Pitfall 7) | medium | mitigate | explicit rp_id and origins config with app.url and frontend_url defaults; the library checks origin and RP ID hash; README documents IP hosts and split domains |
| T-19.6-30 | Elevation of privilege | social sign-in bypassing an enabled second factor (D-09) | high | mitigate | gate in signIn before FinishLogin and before any token; challenge body replaces the token in oauth-complete and oauth-register-complete; errors fail closed; link stays ungated because the caller is signed in; TestSocialGateSmoke |
| T-19.6-31 | Elevation of privilege | passkey login of a banned, suspended or inactive account | high | mitigate | activation check and RejectIfThrottled before the token; one generic failure body; TestPasswordlessSmoke banned_refused |
| T-19.6-32 | Tampering | remote operations (Task 5 and Task 6 pushes) | medium | mitigate | blocking decision first; fetch and compare the unpushed range to this phase's commits before each push; never force; no deploy; fonoteka.go never pushed |
| T-19.6-33 | Tampering | demo rendering the server's QR SVG | low | mitigate | rendered through an img data URL, never v-html (acceptance criterion greps for it) |
| T-19.6-34 | Information disclosure | WebAuthn user handle derived from the numeric user id (A4) | low | accept | PHP-compatible handle keeps imported passkeys mappable; it reveals only an internal id to the user's own authenticator |
| T-19.6-35 | Denial of service | registration and passkey state in the memory cooler on multi-instance deployments (A11) | low | accept | documented in the README: multi-instance applications need the Redis cooler driver; ceremonies fail closed otherwise |
| T-19.6-SC | Tampering | Go module installs | high | mitigate | go-webauthn v0.18.2 installed only after plan 01 Task 5's recorded approval; any other version needs the user's approval; `go list -deps` free of the archived YAML module; fonoteka.go's module graph has no go-webauthn |
</threat_model>
<verification>
- Task 1: TestWebAuthnCeremonySmoke (short), TestWebAuthnSmoke and the plan 02 smokes.
- Task 2: TestPasswordlessSmoke, TestWebAuthnSmoke; TestSocialGateSmoke and sociallogin's TestTracerSmoke.
- Task 3: vitest with twoFactor.test.ts and the Nuxt build.
- Task 4: the reply recorded. Task 5: ls-remote checks. Task 6: fonoteka.go full short suite and build; demo TestMFAMounted, TestBootAllPlugins, TestGoModRequiresTags; bm suite and TestMFAMounted.
- Feedback sampling (19.6-VALIDATION.md): after each commit, vet and the short suite of the touched repository.
</verification>
<success_criteria>
- SC-1: WebAuthn registration and login challenges work with the standard shapes; passkey login behind its setting.
- D-09: social sign-in goes through the challenge only when the setting is on.
- D-17: published in order; demo and bm mount mfa; fonoteka.go green at the new pointer without mfa.
- D-20: the demo's TOTP setup, recovery codes, disable and login challenge screens.
</success_criteria>
<output>
Create `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-03-SUMMARY.md` when done (a draft at the Task 4 checkpoint, final after Task 6), with the commits in order per repository, the Task 4 reply verbatim, the tags and the repository URL, the final identifiers, which Docker-backed tests ran or were NOT RUN, every deviation (for example push-only skipping the demo), and a g15office comment on `avoroeb4` (or a note that the tool was unavailable).
</output>

View File

@@ -0,0 +1,349 @@
---
phase: 19.6-two-factor-auth-and-device-flow-in-sm-user-plugin
plan: 04
type: execute
wave: 4
depends_on: ["19.6-01", "19.6-02", "19.6-03"]
files_modified:
- scripts/check-phase19.6.sh
- ../fonoteka.go/plugins/golem15/user/login_gate_test.go
- ../fonoteka.go/plugins/golem15/user/password_changed_test.go
- ../fonoteka.go/plugins/golem15/user/reset_two_factor_test.go
- ../fonoteka.go/plugins/golem15/user/device_auth_test.go
- ../fonoteka.go/plugins/golem15/user/classes/device_auth_test.go
- ../fonoteka.go/plugins/golem15/user/updates/device_auth_sessions_test.go
- ../sm-mfa-plugin/gate_test.go
- ../sm-mfa-plugin/challenge_test.go
- ../sm-mfa-plugin/totp_test.go
- ../sm-mfa-plugin/recovery_test.go
- ../sm-mfa-plugin/email_test.go
- ../sm-mfa-plugin/trusted_test.go
- ../sm-mfa-plugin/settings_test.go
- ../sm-mfa-plugin/status_test.go
- ../sm-mfa-plugin/admin_test.go
- ../sm-mfa-plugin/tokens_at_rest_test.go
- ../sm-mfa-plugin/migrations_test.go
- ../sm-mfa-plugin/webauthn_test.go
- ../sm-mfa-plugin/passwordless_test.go
- ../sm-mfa-plugin/routes_test.go
- ../sm-mfa-plugin/php_bodies_test.go
- ../sm-mfa-plugin/login_paths_test.go
- ../sm-mfa-plugin/php_method_map_test.go
- ../sm-mfa-plugin/php_port_test.go
- ../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md
- ../sm-mfa-plugin/testdata/php_methods.txt
- ../sm-mfa-plugin/README.md
- ../sm-sociallogin-plugin/social_gate_test.go
- .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md
- .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md
- ../sm-mfa-plugin/go.mod
- ../sm-mfa-plugin/go.sum
- ../sm-sociallogin-plugin/go.mod
- ../sm-sociallogin-plugin/go.sum
- ../sm-demo-app/plugins/golem15/mfa
- ../sm-demo-app/plugins/golem15/user
- ../sm-demo-app/plugins/golem15/sociallogin
- ../sm-demo-app/go.mod
- ../sm-demo-app/go.sum
- ../sm-bm-app/plugins/golem15/mfa
- ../sm-bm-app/plugins/golem15/user
- ../sm-bm-app/plugins/golem15/sociallogin
- ../fonoteka.go/plugins/golem15/user
- .planning/todos/pending/mfa-post-release-fixes.md
autonomous: false
requirements: [SC-4, D-02, D-05, D-06, D-07, D-08, D-09, D-10, D-11, D-12, D-12a, D-12b, D-12c, D-13, D-14, D-15, D-16, D-17, D-18, D-19]
estimate:
tokens: 330000
raw_tokens: 330000
tasks: 6
confidence: low
must_haves:
truths:
- "SC-4: the phase's new code has unit tests delivered in this, the last plan: sm-user-plugin's gate, events, reset slot and device flow; every decision of sm-mfa-plugin in the 19.6-RESEARCH.md decision-to-test map; sociallogin's gate; on Postgres where a database is involved."
- "D-02, D-18 (A10): table tests pin the gate chain (nil registry, order, first result wins, errors fail closed and stop the chain) and Login's order (rehash before the gate, restore and reactivate mail after it, no event on a challenge)."
- "D-11, D-12a, D-12b, D-12c, D-06: table tests cover initiate validation and cleanup, status for every state with the flat expired body and used never expiring, authorize by short code (case, spaces, missing hyphen, 7 and 9 characters, excluded letters) and by token with PHP's 400, 401 and 422 bodies, concurrent authorizes and polls each yielding one winner, the deleted-user hand-over, list order and is_current, revoke of foreign ids, revoke after one and two refreshes, and token columns holding SHA-256 hex."
- "D-05, D-06, D-07, D-10, D-12, D-13, D-15, D-16, D-18, D-19: sm-mfa-plugin tests pin every PHP error and success body verbatim, the challenge lifecycle (TTL, five attempts, one completion under 20 parallel verifies), TOTP window and replay with an injected clock, the QR serializer, recovery format and single use, the email gating, send cap and expiry_minutes, the failure-cap formula and window, trusted devices, every revocation source, the settings overlay and allow-lists, the status shape with the Carbon date form, the admin reset, tokens and secrets at rest, and the six migrations up and down with PHP's column names."
- "D-08, D-14: WebAuthn tests pin the standard option and response shapes (attestation none, timeout 60000, user verification preferred for second factors, required for passkeys), server-side state consumed once, BE and clone refusals, and every passkey rule; 19.6-SECURITY-REVIEW.md has an explicit D-14 section mapping each passkey risk to a passing test."
- "D-09, invariant: TestEveryLoginPathIsGated drives password and social (toggle on) sign-ins of a TOTP user to a challenge with no token, and proves passkey and device-poll tokens arise only through their own proofs; TestSocialGate covers the sociallogin side, link included."
- "PHP test map: every method of the PHP plugin's tests/security/AuthenticationTest.php appears in sm-mfa-plugin/testdata/PHP_METHOD_MAP.md with its Go test or not-applicable with a reason; TestPHPMethodMap fails on a missing, unknown or extra method and on a named Go test that does not exist; test_user_005 maps to TestPHPUser005 (typed mode setting)."
- "Security review: 19.6-SECURITY-REVIEW.md maps every T-19.6 threat of plans 01-04 to the passing test that pins its mitigation or to an accepted disposition with its reason; a defect found is fixed in its own commit with a test."
- "scripts/check-phase19.6.sh fails closed: a required test that fails, skips or does not run fails its stage; --self-test proves the detectors on planted output; --all runs every stage across summercms.go, sm-user-plugin, sm-mfa-plugin, sm-sociallogin-plugin, sm-demo-app (Go and vue-demo-app vitest), sm-bm-app and fonoteka.go, and prints NOT RUN docker without a reachable daemon unless PHASE196_REQUIRE_DOCKER=1 makes that a failure."
- "D-17 after the release: every production fix this plan commits after plan 03's release (a change outside *_test.go, testdata/ and README.md in sm-user-plugin, sm-mfa-plugin or sm-sociallogin-plugin) is contained in every pin that consumes it, through patch tags and separate pointer-bump commits under the Task 5 go-ahead; on hold, each unreleased fix is a recorded deviation with a todo and a g15office task."
artifacts:
- path: "scripts/check-phase19.6.sh"
provides: "fail-closed phase gate across seven repositories"
contains: "PHASE196_REQUIRE_DOCKER"
- path: "../sm-mfa-plugin/login_paths_test.go"
provides: "TestEveryLoginPathIsGated (assumption-delta invariant)"
contains: "TestEveryLoginPathIsGated"
- path: "../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md"
provides: "case-by-case map of the PHP AuthenticationTest methods"
contains: "test_user_005_twofactor_type_confusion"
- path: ".planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md"
provides: "threat-to-test map T-19.6-01..T-19.6-38 and the D-14 review"
contains: "D-14"
key_links:
- from: "scripts/check-phase19.6.sh"
to: "../sm-mfa-plugin/php_method_map_test.go"
via: "the mfa stage requires TestPHPMethodMap by name"
pattern: "TestPHPMethodMap"
- from: ".planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md"
to: "scripts/check-phase19.6.sh"
via: "the per-task map names the gate stage and test for each decision"
pattern: "check-phase19.6.sh"
- from: "../sm-mfa-plugin"
to: "../sm-demo-app/plugins/golem15/mfa"
via: "a post-release fix reaches the applications through Task 6's patch tag and pointer bump, checked with merge-base --is-ancestor"
pattern: "merge-base --is-ancestor"
prohibitions:
- statement: "No production behaviour changes in this plan except fixes for defects a new test or the security review exposes, each in its own commit in the owning repository with the README change it needs."
status: unresolved
verification: judgment
- statement: "Tests never contain a real secret, never call a real authenticator or mail server and never log codes or tokens; WebAuthn runs through the virtual authenticator, mail through the capturing mailer."
status: unresolved
verification: judgment
- statement: "A skipped Docker test is never counted as passed: the gate treats a skip of a required name as a failure, and the SUMMARY records NOT RUN where no daemon answered."
status: unresolved
verification: judgment
- statement: "Test-only commits are pushed only as plan 03's Task 4 reply authorised; tags and pointer bumps happen only in Task 6 under the Task 5 reply (or plan 03's reply when it pre-authorised patch releases); nothing is force-pushed or deployed; fonoteka.go is never pushed."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not in user-story format; no story invented, see plan 01): `sm-user-plugin` has the PHP user plugin's second factors and device login. ROADMAP SC-4: the phase gets a security review and the new code has unit tests, delivered in the phase's last plan (also the project rule "unit tests are the last plan of every phase").
This plan's slice (plan 4 of 4, wave 4, "Unit tests"): one fail-closed gate first (the tracer), then unit tests for sm-user-plugin, then sm-mfa-plugin's and sociallogin's decision-to-test map with the login-path invariant, then the PHP test map, the security review with the explicit D-14 section, the whole gate green and VALIDATION.md completed. If a production fix landed after plan 03's release, a gated patch release carries it to the applications (Tasks 5 and 6).
## Spec-less probe fallback
spec-less probe fallback skipped: phase has no requirement IDs to probe (visible skip). `must_haves` derive from SC-1..SC-4 and D-01..D-20.
## Decisions taken in this plan
- Test names (one top-level test per decision area with table subtests): sm-user-plugin `TestLoginGates` (short), `TestLoginGateOrder`, `TestPasswordChangedSources`, `TestResetTwoFactorAction`, `TestDeviceCodes` (short, classes package), `TestDeviceAuthURL` (short), `TestDeviceDisplayName` (short), `TestDeviceInitiate`, `TestDeviceStatus`, `TestDeviceAuthorize`, `TestDeviceHandOverConcurrent`, `TestDeviceListAndRevoke`, `TestDeviceRevokeAfterRefresh`, `TestDeviceTokensAtRest`, `TestDeviceAuthSessionsMigration` (updates package); sm-mfa-plugin `TestTOTP` (short), `TestQRSVG` (short), `TestRecoveryCodeFormat` (short), `TestMaskEmail` (short), `TestEmailExpiryMinutes` (short), `TestWebAuthnShapes` (short), `TestRoutesAndBuckets` (short), `TestPHPMethodMap` (short), `TestPHPUser005` (short), `TestGate`, `TestChallengeLifecycle`, `TestRecovery`, `TestEmailCode`, `TestFailureCap`, `TestTrustedDevice`, `TestRevocation`, `TestSettings`, `TestStatusShape`, `TestAdminReset`, `TestTokensAtRest`, `TestMigrations`, `TestWebAuthn`, `TestPasswordless`, `TestPHPErrorBodies`, `TestEveryLoginPathIsGated`; sm-sociallogin-plugin `TestSocialGate`. The smoke tests of plans 01-03 stay and are required by the gate too.
- The gate's user-plugin base for the additive check is the commit plan 01 started from (19.6-01-SUMMARY.md; `35cf166` when nothing moved), overridable with PHASE196_USER_BASE. The migration check allows exactly one added non-test file under updates/ (`202610210002_create_device_auth_sessions.go`) and no modified or removed one.
- The PHP map covers the one PHP test file with second-factor content, tests/security/AuthenticationTest.php, method by method (RESEARCH: PHP has almost no tests here); 2FA and device cases map to Go tests, the others to not-applicable with the phase or reason that owns them.
- Post-release fixes (D-17): plan 03 released the plugins and pinned the applications. A defect fix in Tasks 2-4 reaches them only through a new release; Task 4 lists such fixes against every pin; Task 5 asks for the go-ahead unless plan 03's Task 4 reply already authorised patch releases for this plan (then Task 5 records that and continues); Task 6 releases in D-17 order. Test-only and README-only commits need no release.
- Context budget and hand-offs (estimate 330k across 6 tasks; the four-plan decision fixes the plan count): the first agent runs Tasks 1-2 and writes `19.6-04-SUMMARY.md` as a draft (`Status: draft (hand-off after Task 2)`) with commits per repository, test names, gate stages and NOT RUN notes; a fresh agent runs Tasks 3-4 and ends with the `## Post-release fixes` table; when the table is empty it writes "nothing to propagate" under `Task 5 reply`, records Tasks 5 and 6 as not needed and finalizes the SUMMARY, otherwise it stops at Task 5 (or continues when plan 03's reply pre-authorised); a continuation agent runs Task 6.
## Artifacts this phase produces (this plan)
- `scripts/check-phase19.6.sh` with modes `--go`, `--userplugin`, `--mfa`, `--sociallogin`, `--demo`, `--demo-web`, `--bm`, `--fonoteka`, `--docker`, `--docs`, `--self-test`, `--all`; environment `PHASE196_ROOT`, `USER_PLUGIN`, `MFA_PLUGIN`, `SOCIALLOGIN_PLUGIN`, `DEMO_APP`, `DEMO_WEB`, `BM_APP`, `FONOTEKA`, `PHASE196_USER_BASE`, `PHASE196_REQUIRE_DOCKER`.
- Unit tests named in Decisions; PHP map files `../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md` and `testdata/php_methods.txt`; README Testing sections listing them.
- `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md`; the completed `19.6-VALIDATION.md` (status validated, nyquist_compliant true, wave_0_complete true).
- When needed: patch tags of the touched plugins, pointer-bump commits, `.planning/todos/pending/mfa-post-release-fixes.md`.
<objective>
Deliver the phase's unit tests last: a fail-closed gate over every touched repository, decision-by-decision tests for sm-user-plugin, sm-mfa-plugin and the social-login gate, the login-path invariant, the PHP test map, the security review with the explicit passkey section, and the release of any post-release fix.
Purpose: SC-4 and every decision's test row in RESEARCH's Validation Architecture; the D-14 security review; D-17 for fixes.
Output: test commits in sm-user-plugin, sm-mfa-plugin and sm-sociallogin-plugin, the gate script in summercms.go, the security review and validation docs, and (only if needed) a gated patch release.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-CONTEXT.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-RESEARCH.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-01-SUMMARY.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-02-SUMMARY.md
@.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-03-SUMMARY.md
Repos and concurrency: tests are added in sm-user-plugin (../fonoteka.go/plugins/golem15/user), sm-mfa-plugin and sm-sociallogin-plugin; the gate script and the planning docs in summercms.go. A defect a test exposes is fixed in the owning repository in its own commit with its README change and listed in the SUMMARY's `## Post-release fixes` table. No co-author tags; stage by explicit path; test and production changes in separate commits. Keep vet and the short suites green at every commit. Docker-backed tests skip under -short; run them through `sudo -u golem` if the socket is denied; when containers are unreachable (Phase 19.8 saw this on this host) the docker stage reports NOT RUN and the SUMMARY says so.
<interfaces>
- scripts/check-phase19.5.sh (whole file: header, `set -euo pipefail`, the go test -json detector, `--self-test`, docker NOT RUN rule, `--all`) and scripts/check-phase19.4.sh: the structure to copy.
- 19.6-01/02/03 SUMMARYs: final identifiers, smoke test names, migration IDs, tags, the base commit, the Task 4 reply of plan 03.
- sm-user-plugin test helpers: session_test.go (`sessionApp`, `insertUser`, `loginToken`, `bearer`), admin_harness_test.go (`newAdminEnv`, `admin`, `call`, `expect`, `mails`), updates/postgres_test.go (migration harness, `dedicatedDB`), the plan 01 smoke tests.
- sm-mfa-plugin test helpers: harness_test.go (`bootMFA`, `freshDB`), webauthn_authenticator_test.go (`virtualAuthenticator`), the plan 02-03 smoke tests; pquerna `totp.GenerateCodeCustom` for codes at injected times.
- sm-sociallogin-plugin: harness_test.go, fakeprovider_test.go, social_gate_smoke_test.go, testdata/PHP_METHOD_MAP.md and php_method_map_test.go (the map-test pattern to copy).
- PHP: /media/nvme/dev/jz/wavepath.org/plugins/golem15/user/tests/security/AuthenticationTest.php (USER-005 at :132-190); RESEARCH Q4 for every verbatim body.
- RESEARCH Validation Architecture (decision-to-test map, Wave 0 gaps), Security Domain (ASVS categories, known threat patterns), Assumptions A1-A13.
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: scripts/check-phase19.6.sh runs every repository's suite and the phase's smoke tests by name, and fails when one is missing, skipped or red</name>
<files>scripts/check-phase19.6.sh</files>
<read_first>scripts/check-phase19.5.sh (whole file), scripts/check-phase19.4.sh, 19.6-01-SUMMARY.md, 19.6-02-SUMMARY.md, 19.6-03-SUMMARY.md (smoke test names, base commit, tags), 19.6-VALIDATION.md</read_first>
<action>Create scripts/check-phase19.6.sh from check-phase19.5.sh's structure (header comment listing every stage, `set -euo pipefail`, NO_COLOR, the go test -json detector that fails a stage on a failed or skipped test including subtests, zero matched tests, "no tests to run" or a missing required name, a subtest pass never counting for its parent, `--self-test` with planted output, `--all`). Defaults are the sibling checkouts: PHASE196_ROOT (this repository), USER_PLUGIN (../fonoteka.go/plugins/golem15/user, tested through the fonoteka.go workspace), MFA_PLUGIN (../sm-mfa-plugin), SOCIALLOGIN_PLUGIN (../sm-sociallogin-plugin), DEMO_APP (../sm-demo-app), DEMO_WEB (../sm-demo-app/vue-demo-app), BM_APP (../sm-bm-app), FONOTEKA (../fonoteka.go), PHASE196_USER_BASE (the base from 19.6-01-SUMMARY.md). Stages:
- `--go`: go vet ./... and go test -short ./... here (the framework is unchanged by this phase; the stage proves it stays green and that the framework go.mod requires none of the three second-factor libraries).
- `--userplugin`: vet and short tests of the user plugin through the fonoteka.go workspace with the Docker-free names (TestLoginGatesSmoke, TestDeviceCodesSmoke now; Task 2 adds its names); the additive check (no line declaring an exported func, method, type or const removed from a non-test .go file since the base); the migration check (the only non-test change under updates/ is the added device-session migration).
- `--mfa`: vet and short tests in sm-mfa-plugin with TestQRSVGSmoke and TestWebAuthnCeremonySmoke by name; `go list -deps .` free of the archived YAML v3 module; go.mod pins pquerna/otp v1.5.0, boombuler/barcode v1.1.0 and go-webauthn/webauthn at the version the 19.6-03 SUMMARY records.
- `--sociallogin`: vet and short tests in the plugin plus its Phase 19.5 rules (no gorilla/sessions, no goth import outside classes/providers_goth.go).
- `--demo`: go build ./..., go vet ., go test -short . with TestBootAllPlugins, TestGoModRequiresTags and TestMFAMounted by name (skipped with a NOT MOUNTED note when the demo's summer.yaml has no golem15.mfa because plan 03 ran push-only or hold).
- `--demo-web`: `pnpm --dir "$DEMO_WEB" test` must pass and report app/lib/twoFactor.test.ts.
- `--bm`: go build ./..., go vet ., go test -short . with TestSocialLoginMounted and TestMFAMounted by name.
- `--fonoteka`: go vet ./... and go test -short ./... of the workspace; summer.yaml does not mount golem15.mfa; `go list -m all` lists no go-webauthn, pquerna or boombuler module (D-01).
- `--docker` (Docker host only, "needs a reachable Docker daemon" otherwise): the Postgres smoke tests of plans 01-03 by name without -short (user plugin: TestLoginGateSmoke, TestPasswordChangedEventSmoke, TestResetTwoFactorSlotSmoke, TestDeviceFlowSmoke, TestDeviceRevokeSmoke; mfa: TestTracerSmoke, TestChallengeSmoke, TestTrustedSettingsSmoke, TestWebAuthnSmoke, TestPasswordlessSmoke; sociallogin: TestSocialGateSmoke).
- `--docs`: the decision note names the three pins; STACK.md has the three rows; TestDocsTree and `go run ./cmd/summer docs:build --check`; no README of sm-mfa-plugin or sm-user-plugin and no file under modules/ or docs/ names a consuming application (case-insensitive grep for plytarium, płytarium, fonoteka, wavepath, horoskopia, queststream and bm.jakubzych outside _test.go files and .planning).
- `--all`: self-test, go, userplugin, mfa, sociallogin, demo, demo-web, bm, fonoteka, docs, then docker when a daemon answers (NOT RUN docker otherwise; a failure with PHASE196_REQUIRE_DOCKER=1).
Each later task appends its test names to the stage that runs them. `--self-test` covers the detectors (fail, skip, zero tests, missing name, a subtest pass not counting for its parent, a vitest run without the required file) and the docker NOT RUN rule on a faked PATH without docker. Commit in summercms.go: `test(19.6): fail-closed phase gate`.</action>
<verify>
<automated>bash -n scripts/check-phase19.6.sh &amp;&amp; scripts/check-phase19.6.sh --self-test &amp;&amp; scripts/check-phase19.6.sh --go &amp;&amp; scripts/check-phase19.6.sh --userplugin &amp;&amp; scripts/check-phase19.6.sh --mfa &amp;&amp; scripts/check-phase19.6.sh --sociallogin</automated>
<fails_when>Non-zero exit: a syntax error, a self-test detector that does not catch its planted failure, or a stage that finds a required smoke test missing, skipped or red, a removed user-plugin export, a modified user-plugin migration, an unexpected library version or the archived YAML module in the mfa dependency graph.</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'PHASE196_REQUIRE_DOCKER' scripts/check-phase19.6.sh` prints at least 2.
- `grep -cE -- '^\s*--(go|userplugin|mfa|sociallogin|demo|demo-web|bm|fonoteka|docker|docs|self-test|all)\)' scripts/check-phase19.6.sh` prints at least 12.
- `grep -c 'TestWebAuthnCeremonySmoke' scripts/check-phase19.6.sh` prints at least 1 and `grep -c '202610210002_create_device_auth_sessions' scripts/check-phase19.6.sh` prints at least 1.
- `test -x scripts/check-phase19.6.sh` exits 0.
</acceptance_criteria>
<done>One command shows, for every repository the phase touched, whether its suite and the phase's named tests pass, and a skipped or missing test cannot pass it.</done>
</task>
<task type="auto">
<name>Task 2: Unit tests for sm-user-plugin's additions (gate chain and Login order, password-changed sources, reset action, the whole device flow and its migration), wired into the gate</name>
<files>../fonoteka.go/plugins/golem15/user/login_gate_test.go, ../fonoteka.go/plugins/golem15/user/password_changed_test.go, ../fonoteka.go/plugins/golem15/user/reset_two_factor_test.go, ../fonoteka.go/plugins/golem15/user/device_auth_test.go, ../fonoteka.go/plugins/golem15/user/classes/device_auth_test.go, ../fonoteka.go/plugins/golem15/user/updates/device_auth_sessions_test.go, ../fonoteka.go/plugins/golem15/user/README.md, scripts/check-phase19.6.sh</files>
<read_first>19.6-01-SUMMARY.md, ../fonoteka.go/plugins/golem15/user/classes/login_gate.go, ../fonoteka.go/plugins/golem15/user/controllers/api_controller.go (Login, Refresh), ../fonoteka.go/plugins/golem15/user/controllers/devices.go, ../fonoteka.go/plugins/golem15/user/classes/device_auth.go, ../fonoteka.go/plugins/golem15/user/controllers/users_admin_controller.go, the plan 01 smoke tests, ../fonoteka.go/plugins/golem15/user/session_test.go, ../fonoteka.go/plugins/golem15/user/admin_harness_test.go, ../fonoteka.go/plugins/golem15/user/updates/postgres_test.go, ../fonoteka.go/plugins/golem15/user/updates/api_tokens_test.go (migration test style), 19.6-RESEARCH.md (Q4 device flow, Q7, Pitfalls 3, 13-17, 21)</read_first>
<action>Tests only (fix defects in separate commits). `TestLoginGates` (short): nil registry, empty, order, first result wins, error stops the chain, concurrent Add and Check under `go test -race` clean. `TestLoginGateOrder` (Postgres): the rehash happens for a challenged user (stored hash changes when bcrypt_cost rises, no PasswordChangedEvent); a challenged soft-deleted user stays soft-deleted with no mail; an allowed one is restored with one reactivate mail and one LoginEvent; a refusal result's status and body pass through; refresh never calls a gate (a counting gate stays at 0 across /refresh). `TestPasswordChangedSources` (Postgres): reset, change and admin set each fire once with their source inside a transaction (the listener sees the new hash through ev.Tx), rehash and register fire none, a listener error rolls back each of the three writes. `TestResetTwoFactorAction` (admin harness): not offered without a service; offered and Danger with one; HasTwoFactor false hides it; the permission is required; Run without a transaction errors; Run's error rolls back. `TestDeviceCodes` (classes package, short): token length, alphabet and uniqueness over 10,000 draws, short-code format and distribution (every alphabet character appears), normalization table. `TestDeviceAuthURL` and `TestDeviceDisplayName` (short tables, including the PHP accessor order iPhone before Mac). `TestDeviceInitiate` (device_name over 255 characters 422; the cleanup deletes only stale pending and expired rows and at most 500; short-code collision retry with a pre-inserted code). `TestDeviceStatus` (unknown 404 body; pending; expired pending and expired confirmed give the flat body and set status expired; used and revoked past expires_at keep their status, Pitfall 13). `TestDeviceAuthorize` (short code variants and refusals with PHP bodies; token path; a session bound to another user refused; the same caller re-authorizing answers success; 10 concurrent authorizes by two users bind exactly one; missing, invalid and blacklisted bearer 401 with {"error":"Unauthorized"}). `TestDeviceHandOverConcurrent` (50 concurrent polls give one token; the deleted-user hand-over marks revoked and answers status revoked; LoginEvent Source device fired once). `TestDeviceListAndRevoke` (order, is_current, device_name, foreign id 404, non-numeric id unmatched, idempotent revoke, empty list is []). `TestDeviceRevokeAfterRefresh` (one and two refreshes before revoke; the blacklist entry outlives the refresh window; a refresh failure of the device token leaves the row unchanged). `TestDeviceTokensAtRest` (the token column equals SHA-256 hex and never the plaintext; no plaintext in any column). `TestDeviceAuthSessionsMigration` (updates package, dedicated database: up creates PHP's column names plus jwt_jti, the unique token and short_code, the CHECK refuses an unknown status, the FK cascades on user delete, rollback drops the table, re-up works). Append every name to the userplugin and docker stages of the gate; README Testing lists them. Commits in sm-user-plugin: `test: login gates, password-changed sources and the reset action` and `test: device authorization flow and its migration`; gate update in summercms.go: `test(19.6): gate requires the user-plugin unit tests`.</action>
<verify>
<automated>scripts/check-phase19.6.sh --userplugin &amp;&amp; go -C ../fonoteka.go test -count=1 -short -race -run '^(TestLoginGates|TestDeviceAuthURL|TestDeviceDisplayName)$' -v ./plugins/golem15/user 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>Non-zero exit from the userplugin stage, or the count of top-level "--- PASS: Test" lines is not 3 (a short table test failed, raced or is missing).</fails_when>
<automated>scripts/check-phase19.6.sh --docker</automated>
<fails_when>Non-zero exit: a required Postgres test failed or skipped; "needs a reachable Docker daemon" means NOT RUN, recorded in the SUMMARY (rerun through sudo -u golem first).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '^func TestDeviceRevokeAfterRefresh' ../fonoteka.go/plugins/golem15/user/device_auth_test.go` prints 1 and `grep -c '^func TestDeviceHandOverConcurrent' ../fonoteka.go/plugins/golem15/user/device_auth_test.go` prints 1.
- `grep -c '^func TestDeviceAuthSessionsMigration' ../fonoteka.go/plugins/golem15/user/updates/device_auth_sessions_test.go` prints 1.
- `grep -c 'TestDeviceRevokeAfterRefresh' scripts/check-phase19.6.sh` prints at least 1 and `grep -c 'TestLoginGateOrder' scripts/check-phase19.6.sh` prints at least 1.
- `grep -c 'TestDeviceRevokeAfterRefresh' ../fonoteka.go/plugins/golem15/user/README.md` prints at least 1.
</acceptance_criteria>
<done>Every user-plugin seam and every device-flow state and refusal is pinned by a named test the gate requires.</done>
</task>
<task type="auto">
<name>Task 3: Unit tests for sm-mfa-plugin and the social-login gate, one per decision in the validation map, plus the invariant that every first-factor login path is gated, wired into the gate</name>
<files>../sm-mfa-plugin/gate_test.go, ../sm-mfa-plugin/challenge_test.go, ../sm-mfa-plugin/totp_test.go, ../sm-mfa-plugin/recovery_test.go, ../sm-mfa-plugin/email_test.go, ../sm-mfa-plugin/trusted_test.go, ../sm-mfa-plugin/settings_test.go, ../sm-mfa-plugin/status_test.go, ../sm-mfa-plugin/admin_test.go, ../sm-mfa-plugin/tokens_at_rest_test.go, ../sm-mfa-plugin/migrations_test.go, ../sm-mfa-plugin/webauthn_test.go, ../sm-mfa-plugin/passwordless_test.go, ../sm-mfa-plugin/routes_test.go, ../sm-mfa-plugin/php_bodies_test.go, ../sm-mfa-plugin/login_paths_test.go, ../sm-mfa-plugin/README.md, ../sm-sociallogin-plugin/social_gate_test.go, scripts/check-phase19.6.sh</files>
<read_first>19.6-02-SUMMARY.md, 19.6-03-SUMMARY.md, every classes/ and controllers/ file of ../sm-mfa-plugin, ../sm-mfa-plugin/harness_test.go, ../sm-mfa-plugin/webauthn_authenticator_test.go, the plan 02-03 smoke tests, ../sm-sociallogin-plugin/classes/flow.go, ../sm-sociallogin-plugin/social_gate_smoke_test.go, 19.6-RESEARCH.md (Q4 every body and table, Validation Architecture decision-to-test map, Pitfalls 1-12, Assumptions A2, A5-A9)</read_first>
<action>Tests only (fix defects in separate commits, listed for Task 4's table). Short tests: `TestTOTP` (injected times at step boundaries: counters at-1, at, at+1 accepted, at+2 and at-2 refused, replay at or below last_counter refused, the URL's issuer, account, algorithm, digits and period), `TestQRSVG` (several lengths and levels round-trip to the matrix; size attributes), `TestRecoveryCodeFormat` (10 codes, regex, alphabet only, no duplicates across 1,000 sets' codes within a set, normalization and the 32-character cap), `TestMaskEmail` (PHP table including one- and two-character local parts and plus addresses), `TestEmailExpiryMinutes` (A9: min of TTL and remaining time, rounded up, never below 1), `TestWebAuthnShapes` (D-08: the marshalled options have publicKey with a base64url challenge, attestation none, timeout 60000, userVerification preferred for second factors and required with residentKey required for passkeys, and the user id is the 8-byte big-endian handle), `TestRoutesAndBuckets` (boot without a database: every challenge and passwordless route carries the 2fa-verify bucket, every management route jwt.auth and throttle:user-api, each {id} route has the digit constraint; the 2fa-verify bucket is 10 per minute keyed by client IP).
Postgres tests: `TestGate` (sources password and social with the toggle on and off; passkey, two_factor and device never challenge; mode disabled exempts even users with methods and nothing else does, D-13; no enabled method passes; a user with only a webauthn row and no credential is challenged with available_methods [] as PHP; available_methods not filtered by the global list; the cap refusal); `TestChallengeLifecycle` (TTL from the setting; five attempts then refusal; 20 parallel verifies give one token; completed and expired challenges refused; creation keeps rows inside the cap window and deletes older expired ones); `TestRecovery` (single use under 10 concurrent attempts; regenerate replaces in one transaction; count route requires the password; D-19 recovery_codes only when a set was created, for totp/confirm, email/enable and webauthn/register); `TestEmailCode` (A8 gating for send and verify; three sends then refusal; each send replaces the code; template name per locale and the vars in both cases; the code is never in a log line, using a capturing slog handler); `TestFailureCap` (the formula over completed and uncompleted rows, the window boundary, refusal at login and on every verify endpoint, recovery after the window); `TestTrustedDevice` (D-10 issue, skip, expiry, other user, disabled setting, last_used_at and ip update, social never skipped, A7); `TestRevocation` (D-07: change, reset and admin set delete trusted devices; rehash does not; last method disable deletes devices and codes; removing the last WebAuthn credential does the same); `TestSettings` (D-12 overlay per key, NULL falls back to config, immediate effect, Rules and BeforeValidate allow-lists, admin JSON of the settings screen); `TestStatusShape` (PHP keys exactly; webauthn_credentials and trusted_devices item keys; the Carbon date form for trusted devices, A2; +00:00 for credentials); `TestAdminReset` (D-15 through the user plugin's record action end to end in the admin harness or through the published service: methods disabled with secrets cleared, credentials, codes and devices deleted, one disabled event, login then answers a token); `TestTokensAtRest` (D-06: challenge and trusted tokens SHA-256 hex, TOTP secret ciphertext that decrypts to the secret, recovery and email codes bcrypt); `TestMigrations` (D-16: the six migrations up, PHP column names and the added columns, indexes and FKs, rollback in reverse, re-up); `TestWebAuthn` (register and verify, state consumed once, wrong origin refused, BE change refused, clone refused, credential_data written back, delete scoped to the owner); `TestPasswordless` (D-14 table: setting off 403; second-factor credential refused; UV missing refused; disabled credential refused; userHandle of another user refused; banned, suspended and inactive refused with the generic body; soft-deleted owner restored and mailed; login event Source passkey; no challenge row created); `TestPHPErrorBodies` (every PHP status and body from RESEARCH Q4's two tables, byte for byte after trimming a trailing newline, including the 422 first-message form); `TestEveryLoginPathIsGated` (the assumption-delta invariant: for a TOTP user, password login and social sign-in with the toggle on answer two_factor_required and no token, social with the toggle off and every non-first-factor source answer tokens only through their own proof: passwordless only with a passkey assertion, the device poll only after an authorize with a bearer JWT; registering a new account and activate-by-code mint without a gate because the account cannot have 2FA yet). sociallogin `TestSocialGate` (refusal, error, challenge payload through oauth-complete and oauth-register-complete, link never gated, token cookie mode not applied to a challenge). Append every name to the mfa, sociallogin and docker stages; README Testing sections list them. Commits: in sm-mfa-plugin `test: second-factor decisions, PHP bodies and the login-path invariant` (split by area if a commit grows past one logical change); in sm-sociallogin-plugin `test: social sign-in gate`; in summercms.go `test(19.6): gate requires the mfa and social gate tests`.</action>
<verify>
<automated>scripts/check-phase19.6.sh --mfa &amp;&amp; scripts/check-phase19.6.sh --sociallogin &amp;&amp; go -C ../sm-mfa-plugin test -count=1 -short -run '^(TestTOTP|TestQRSVG|TestRecoveryCodeFormat|TestMaskEmail|TestEmailExpiryMinutes|TestWebAuthnShapes|TestRoutesAndBuckets)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 7</automated>
<fails_when>Non-zero exit from a stage, or the count of top-level "--- PASS: Test" lines is not 7 (a short decision test failed or is missing).</fails_when>
<automated>scripts/check-phase19.6.sh --docker</automated>
<fails_when>Non-zero exit: a required Postgres test failed or skipped; "needs a reachable Docker daemon" means NOT RUN, recorded in the SUMMARY (rerun through sudo -u golem first).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '^func TestEveryLoginPathIsGated' ../sm-mfa-plugin/login_paths_test.go` prints 1 and `grep -c '^func TestPHPErrorBodies' ../sm-mfa-plugin/php_bodies_test.go` prints 1.
- `grep -c '^func TestPasswordless' ../sm-mfa-plugin/passwordless_test.go` prints 1 and `grep -c '^func TestFailureCap' ../sm-mfa-plugin/challenge_test.go ../sm-mfa-plugin/email_test.go ../sm-mfa-plugin/gate_test.go | awk -F: '{s+=$2} END {print s}'` prints 1.
- `grep -c '^func TestSocialGate' ../sm-sociallogin-plugin/social_gate_test.go` prints 1.
- `grep -c 'TestEveryLoginPathIsGated' scripts/check-phase19.6.sh` prints at least 1 and `grep -c 'TestSocialGate' scripts/check-phase19.6.sh` prints at least 1.
</acceptance_criteria>
<done>Every second-factor decision, every PHP body and the rule that no first-factor path skips the challenge are pinned by named tests the gate requires.</done>
</task>
<task type="auto">
<name>Task 4: The PHP security test mapped method by method, the security review with the explicit passkey section, the whole gate green across seven repositories, and the validation map completed</name>
<files>../sm-mfa-plugin/php_method_map_test.go, ../sm-mfa-plugin/php_port_test.go, ../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md, ../sm-mfa-plugin/testdata/php_methods.txt, ../sm-mfa-plugin/README.md, .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md, .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md, scripts/check-phase19.6.sh</files>
<read_first>/media/nvme/dev/jz/wavepath.org/plugins/golem15/user/tests/security/AuthenticationTest.php (whole file), ../sm-sociallogin-plugin/php_method_map_test.go and testdata/PHP_METHOD_MAP.md (pattern), .planning/phases/19.5-social-login-in-sm-user-plugin/19.5-SECURITY-REVIEW.md (structure), the threat_model blocks of 19.6-01..04-PLAN.md, 19.6-VALIDATION.md, 19.6-RESEARCH.md (Security Domain, Assumptions), 19.6-01/02/03 SUMMARYs</read_first>
<action>PHP map. testdata/php_methods.txt lists every `test_*` method of AuthenticationTest.php (extracted with grep at execution, one per line). testdata/PHP_METHOD_MAP.md has one row per method: PHP method | repository | Go test (top-level or Name/subtest) or not-applicable | reason. test_user_005_twofactor_type_confusion maps to `TestPHPUser005` in php_port_test.go (short): a stored or submitted mode that is not one of the three strings is refused by validation and treated as optional by the effective settings, so enforcement never silently turns off through a type mix-up, and the mode compare is on a typed string. The remaining methods map to the Go test or Phase 19.5 test that covers them, or to not-applicable with the reason (for example PIN login not ported; Phase 07 deferral). `TestPHPMethodMap` (short) fails when a method in php_methods.txt has no row, a row names a method not in the file, or a named Go test does not exist in the named repository's test sources (USER_PLUGIN and SOCIALLOGIN_PLUGIN paths overridable by environment, as sociallogin's map test does).
Security review `19.6-SECURITY-REVIEW.md` (19.5 structure): scope and method; one row per threat T-19.6-01..T-19.6-38 from the four plans' threat_model blocks with the mitigation as built, the passing test that pins it (name and repository) or the accepted disposition with its reason; an explicit "D-14 passwordless passkey login" section reviewing the setting default, credential eligibility (passwordless column, UV at registration and login, credProps rk), user-handle binding, enabled and ownership checks, activation, ban and suspension, generic failures, single-use state and the multi-instance caveat, origin and RP ID checks, clone and BE refusal, rate limiting, deactivated-account restore, logging and why no second challenge follows, each pinned to a TestPasswordless or TestWebAuthn subtest; a "D-18 hardening" section for A6, A8, A9 and A10; ASVS L1 V2, V3, V4, V5, V6 and V7 notes from RESEARCH's Security Domain; residual risks (device-code phishing accepted per D-12c, blacklist_grace forks, memory cooler single instance, numeric user handle). A defect found here is fixed in its own commit with a test and listed for the table below.
Gate and validation. Add TestPHPMethodMap and TestPHPUser005 to the mfa stage; run `scripts/check-phase19.6.sh --all` until green (docker NOT RUN allowed only when no daemon answers, recorded). Update 19.6-VALIDATION.md: status validated, nyquist_compliant true, wave_0_complete true, the per-task verification map with one row per task of plans 01-04 (task id, plan, wave, decision, threat ref, secure behaviour, test type, the gate stage and test name, status), Wave 0 items checked, sign-off ticked. In the SUMMARY write `## Post-release fixes`: one row per production commit made by this plan after plan 03's release (repository, commit, files, the released pins that lack it: user tag and the gitlinks in sm-demo-app, sm-bm-app and fonoteka.go; mfa tag and the demo and bm gitlinks; sociallogin tag and the demo and bm gitlinks), or "none". Commits: in sm-mfa-plugin `test: PHP security test map`; in summercms.go `docs(19.6): security review and validation map` (planning docs alone) and the gate update `test(19.6): gate requires the PHP map`. Post a short comment on g15office task `avoroeb4` with the gate result (or record that the tool is unavailable).</action>
<verify>
<automated>scripts/check-phase19.6.sh --all</automated>
<fails_when>Non-zero exit: any stage failed, a required test is missing or skipped, the PHP map is incomplete, or docker failed under PHASE196_REQUIRE_DOCKER=1 (without it, "NOT RUN docker" in the output means no daemon answered and is recorded in the SUMMARY).</fails_when>
<automated>go -C ../sm-mfa-plugin test -count=1 -short -run '^(TestPHPMethodMap|TestPHPUser005)$' -v . 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>The count of top-level "--- PASS: Test" lines is not 2: the map test or the USER-005 port failed or does not exist.</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'test_user_005_twofactor_type_confusion' ../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md` prints 1 and `grep -c 'TestPHPUser005' ../sm-mfa-plugin/testdata/PHP_METHOD_MAP.md` prints at least 1.
- `grep -cE 'T-19\.6-(0[1-9]|[1-3][0-9])' .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md` prints at least 38 and `grep -c 'D-14' .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-SECURITY-REVIEW.md` prints at least 1.
- `grep -c 'nyquist_compliant: true' .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md` prints 1 and `grep -c 'check-phase19.6.sh' .planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-VALIDATION.md` prints at least 4.
- The SUMMARY has a `## Post-release fixes` heading with a table or the word none.
</acceptance_criteria>
<done>The PHP test file is accounted for method by method, every threat is pinned or consciously accepted, the passkey route has its explicit review, and one command proves the phase across every repository.</done>
</task>
<task type="checkpoint:decision" gate="blocking-human">
<name>Task 5: Go-ahead to release this plan's post-release fixes to the applications that pin plan 03's release (presented only when Task 4's Post-release fixes table has a row and plan 03's reply did not pre-authorise patch releases)</name>
<read_first>19.6-04-SUMMARY.md draft (Post-release fixes table), 19.6-03-SUMMARY.md (Task 4 reply, tags), CLAUDE.md (Commits and concurrency)</read_first>
<acceptance_criteria>
- The SUMMARY records the reply verbatim under "Task 5 reply", or "nothing to propagate" when the table is empty, or "pre-authorised by plan 03 Task 4" with the quoted reply.
- Before the reply, no new tag exists on the plugins: `T=$(git -C ../sm-mfa-plugin tag --points-at HEAD) && test -z "$T" || test "$T" = "v0.1.0"` exits 0 when HEAD is the released commit.
</acceptance_criteria>
<decision>May the post-release fixes listed in the SUMMARY ship as patch releases now?</decision>
<context>Each row of the Post-release fixes table is a production commit in sm-user-plugin, sm-mfa-plugin or sm-sociallogin-plugin made after plan 03 released them; the applications' pins (sm-demo-app tags and gitlinks, sm-bm-app gitlinks, fonoteka.go's user gitlink) do not contain it yet. Releasing means: push the plugin, an annotated patch tag, the dependent plugin's require bump if any, then separate pointer-bump commits in the applications, pushed (fonoteka.go committed locally only). Holding leaves the fixes unreleased with a todo and a g15office task.</context>
<options>
<option id="release">
<name>release (optionally naming the patch tags)</name>
<pros>Every application runs the fixed code; the gate's pin check passes.</pros>
<cons>More tags and pointer bumps today.</cons>
</option>
<option id="hold">
<name>hold</name>
<pros>Nothing leaves this machine.</pros>
<cons>Applications keep the unfixed release; recorded as a deviation with .planning/todos/pending/mfa-post-release-fixes.md and a g15office task.</cons>
</option>
</options>
<resume-signal>Reply "release" (optionally with tags) or "hold". Never auto-approved: tags and pushes always need the user.</resume-signal>
</task>
<task type="auto">
<name>Task 6: Under the Task 5 reply, every post-release fix reaches each application pin in D-17 order: patch tags, dependent requires, then separate pointer-bump commits in sm-demo-app, sm-bm-app and fonoteka.go</name>
<precondition>19.6-04-SUMMARY.md records "release" (or the pre-authorisation) under Task 5 reply; on "hold" or "nothing to propagate" this task only writes the todo (hold) or records "not needed".</precondition>
<files>../sm-mfa-plugin/go.mod, ../sm-mfa-plugin/go.sum, ../sm-sociallogin-plugin/go.mod, ../sm-sociallogin-plugin/go.sum, ../sm-demo-app/plugins/golem15/mfa, ../sm-demo-app/plugins/golem15/user, ../sm-demo-app/plugins/golem15/sociallogin, ../sm-demo-app/go.mod, ../sm-demo-app/go.sum, ../sm-bm-app/plugins/golem15/mfa, ../sm-bm-app/plugins/golem15/user, ../sm-bm-app/plugins/golem15/sociallogin, ../fonoteka.go/plugins/golem15/user, .planning/todos/pending/mfa-post-release-fixes.md</files>
<read_first>19.6-04-SUMMARY.md draft (table, reply), 19.6-03-SUMMARY.md (tags and pins), 19.6-03-PLAN.md Tasks 5 and 6 (the same procedures), .planning/G15OFFICE.md</read_first>
<action>On release, per row and in D-17 order: sm-user-plugin first (push, annotated patch tag, push tag), then sm-mfa-plugin and sm-sociallogin-plugin (require the new user tag when the fix is there, `go mod tidy`, vet and short tests, commit, push, patch tag), then the applications in separate commits (`chore: bump <plugin> to <tag>`): sm-demo-app (submodule checkout of the tag, go.mod require and go.sum), sm-bm-app (gitlink), fonoteka.go (user gitlink only, local commit, never pushed); push sm-demo-app and sm-bm-app after the same pre-push check as plan 03 Task 5 (fetch, compare the unpushed range to this phase's commits, never force, never deploy). Then for every fix commit F and every pin P listed in its row run `git merge-base --is-ancestor F <pin commit>` in the plugin repository and record the results. On hold: write .planning/todos/pending/mfa-post-release-fixes.md (the table, the pins lacking each fix, the commands to release them) with a g15office task under the Phase 19.6 milestone task `avoroeb4`, and record each fix as a deviation in the SUMMARY.</action>
<verify>
<automated>scripts/check-phase19.6.sh --demo &amp;&amp; scripts/check-phase19.6.sh --bm &amp;&amp; scripts/check-phase19.6.sh --fonoteka</automated>
<fails_when>Non-zero exit: an application no longer builds or passes at the new pins, a required test is missing, or fonoteka.go gained a second-factor module. On hold or nothing-to-propagate this command still runs and must pass at the existing pins.</fails_when>
</verify>
<acceptance_criteria>
- On release: for every row, `git -C <plugin repo> merge-base --is-ancestor <fix commit> <pinned commit>` exits 0 for each pin (results listed in the SUMMARY), and `L=$(git -C ../sm-demo-app log --oneline origin/master..master) && test -z "$L"` exits 0 after the push.
- On hold: `test -f .planning/todos/pending/mfa-post-release-fixes.md` exits 0 and the SUMMARY names the g15office task.
</acceptance_criteria>
<done>No application runs a release that lacks a fix this phase made, or the gap is tracked as a todo with its release commands.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| test results to the phase verdict | a skipped, missing or misnamed test must not read as a pass |
| released plugins to application pins | a fix must reach every application that pins the released code |
| new login paths to the gate | any future token mint that skips the second factor must be caught |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6-36 | Tampering | scripts/check-phase19.6.sh verdict | medium | mitigate | the go test -json detector fails on fail, skip, zero matches, "no tests to run" and missing names, a subtest pass never counts for its parent, the vitest detector needs the named file; --self-test proves each on planted output; docker NOT RUN is explicit and fatal with PHASE196_REQUIRE_DOCKER=1 |
| T-19.6-37 | Tampering | post-release fixes not reaching applications | medium | mitigate | Task 4's table lists every production fix against every pin; Task 6 releases in D-17 order under the Task 5 reply and checks each pin with merge-base --is-ancestor; hold records a todo and a g15office task |
| T-19.6-38 | Elevation of privilege | a token mint path that bypasses the second factor (assumption-delta debt) | high | mitigate | TestEveryLoginPathIsGated drives every first-factor path to a challenge and documents each ungated mint by decision; the security review lists the accepted debt from plan 01's assumption_delta_decision |
| T-19.6-SC | Tampering | Go module installs | high | mitigate | this plan installs no module; the mfa stage pins the three approved versions and refuses the archived YAML module; the fonoteka stage refuses second-factor modules in fonoteka.go's graph |
</threat_model>
<verification>
- Task 1: the gate's syntax, self-test and the go, userplugin, mfa and sociallogin stages.
- Task 2: userplugin stage with the new names; short table tests under -race; docker stage (or NOT RUN).
- Task 3: mfa and sociallogin stages; seven short decision tests; docker stage (or NOT RUN).
- Task 4: `scripts/check-phase19.6.sh --all`; TestPHPMethodMap and TestPHPUser005; the review and the validation map.
- Task 5: the reply recorded. Task 6: demo, bm and fonoteka stages at the final pins.
- Feedback sampling (19.6-VALIDATION.md): after each commit, vet and the short suite of the touched repository.
</verification>
<success_criteria>
- SC-4: every new piece of the phase has unit tests in the last plan, and the phase has its security review with the explicit D-14 section.
- The gate passes across all seven repositories (docker NOT RUN only where no daemon answers, recorded).
- The PHP security test file is mapped method by method.
- No application pins a release that lacks a fix, or the gap is a tracked todo.
</success_criteria>
<output>
Create `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-04-SUMMARY.md` when done, with the commits in order per repository, the test names added, each gate stage and its state, the Docker NOT RUN notes, the defects found with their fix commits, the Post-release fixes table, the Task 5 reply verbatim, the merge-base results, and the g15office comment.
</output>

View File

@@ -102,6 +102,15 @@ Repos: `sm-mfa-plugin` (new), `sm-user-plugin`, `sm-sociallogin-plugin` (it call
### Adoption
- **D-17:** **sm-demo-app and sm-bm-app** mount mfa in this phase, and both get the device flow through the user plugin pin bump. fonoteka.go does not mount mfa. Its guard test `fonoteka/phase07_coverage_test.go:79` (`TestNoDeferredRoutesResolve`) fails once `/devices` routes resolve, so it is updated in the fonoteka pointer-bump commit. Commit order: sm-user-plugin → sm-mfa-plugin and sociallogin pushed → app pointer bumps as separate commits. Push, tag and repo creation need the user's go-ahead.
### Decided after research (2026-10-21)
- **D-18:** **Security hardening beyond PHP, all four adopted** (RESEARCH A6, A8, A9, A10):
- A6: a per-user second-factor failure cap: 10 failed verifications in 15 minutes refuse new challenges and verifies with the generic 401 until the window passes. It is counted from challenge rows in the DB, so it holds across instances. Failed challenge rows are kept for the window.
- A8: `email/send` and `verify method=email` work only when the user's email method is enabled; otherwise they get the same 401 `{"error":"Invalid or expired challenge."}`. At most 3 sends per challenge.
- A9: the mail's `expiry_minutes` is `min(email code TTL, remaining challenge time)`.
- A10: restoring a soft-deleted account moves after the 2FA gate, so the password alone cannot reactivate an account. This refines D-02's gate placement: the password step runs first, then the gate, then the undelete and the reactivate mail.
- **D-19:** **The first recovery-code set is returned.** When an enable or confirm call creates a new set, its response carries an additive `recovery_codes` key. Responses that create no set are unchanged from PHP.
- **D-20:** **The demo gets a 2FA UI** in `vue-demo-app` (`../vue-demo-app`, served by sm-demo-app): TOTP setup with the QR code and confirm, the recovery codes shown once, disable, and the login challenge step (TOTP or recovery code). sm-bm-app stays API-only. WebAuthn, email codes and trusted devices in the demo UI are optional.
### Claude's Discretion
- Go identifiers for the gate interface, registry and payloads.
- The QR library (research, D-04).

View File

@@ -0,0 +1,544 @@
# Phase 19.6: Two-factor auth and device flow - Pattern Map
**Mapped:** 2026-10-21
**Files analyzed:** 38 new or modified files in 6 repos
**Analogs found:** 36 / 38
Path roots used below (all siblings of `summercms.go`, under `/media/nvme/dev/golem15/summercms.io/summercms/`):
- `USER` = `fonoteka.go/plugins/golem15/user` (the sm-user-plugin checkout you edit and push from, v0.1.5 / `35cf166`; sociallogin's `go.work` already replaces the module with this path)
- `SOC` = `sm-sociallogin-plugin` (the template for the new plugin)
- `MFA` = `sm-mfa-plugin` (new repo, plugin `golem15.mfa`)
- `DEMO` = `sm-demo-app`, `BM` = `sm-bm-app`, `VDEMO` = `vue-demo-app` (mounted in DEMO as the `vue-demo-app` submodule)
All analog paths are git-tracked in their own repos (checked with `git ls-files`).
## File Classification
### sm-user-plugin (additive seams + device flow)
| New/Modified File | Role | Data Flow | Closest Analog | Match Quality |
|---|---|---|---|---|
| `USER/classes/login_gate.go` (new) | service registry | request-response | `USER/classes/oauth_providers.go` (published seam interface) | role-match |
| `USER/classes/events.go` (mod: `LoginEvent`, `PasswordChangedEvent`, `TwoFactorAdmin` iface) | model/event | event-driven | same file, `DeactivatingEvent` :59-67 | exact |
| `USER/controllers/registration.go` (mod: `FireLoginEvent`, `FirePasswordChangedEvent`) | utility | event-driven | same file, `FireDeactivatingEvent` :166-171 | exact |
| `USER/controllers/api_controller.go` (mod: Login gate :86-115, Refresh JTI hand-over :194-211, ResetPassword :327-343 / ChangePassword :580-597 fire hook) | controller | request-response | itself | exact |
| `USER/controllers/users_admin_controller.go` (mod: `reset_two_factor` action, admin password hook :238-247) | controller (cabana) | CRUD | same file `AdminRecordActions` :662-720 | exact |
| `USER/controllers/users/config_form.yaml` (mod: `recordActions`) | config | — | itself :17 | exact |
| `USER/admin_permissions.go` (mod: `golem15.users.reset_two_factor`) | config | — | itself, `manage_privileged_groups` entry | exact |
| `USER/controllers/devices.go` (new: initiate/status/authorize/list/revoke) | controller | request-response + CRUD | `USER/controllers/api_tokens.go` | exact |
| `USER/models/device_auth_session.go` (new) | model | CRUD | `USER/models/api_token.go` | exact |
| `USER/updates/2026102X0001_create_device_auth_sessions.go` (new) | migration | — | `USER/updates/202610040001_create_api_tokens.go` | exact |
| `USER/classes/device_auth.go` (new: token/short-code gen, hash, state UPDATEs) | service | CRUD | `USER/classes/api_tokens.go` (`MintAPIToken`, `HashAPIToken`) | exact |
| `USER/routes.go` (mod: 5 device routes) | route | — | itself :17-37 | exact |
| `USER/config/config.yaml` (mod: `device_auth.auth_url`, ttl) | config | — | itself | exact |
| `USER/README.md` (mod) | docs | — | itself | exact |
### sm-mfa-plugin (new repo)
| New File | Role | Data Flow | Closest Analog | Match Quality |
|---|---|---|---|---|
| `MFA/plugin.go` | plugin entry | — | `SOC/plugin.go` | exact |
| `MFA/routes.go` | route | — | `SOC/routes.go` | exact |
| `MFA/buckets.go` (`2fa-verify`) | middleware config | request-response | `sm-oauthserver-plugin/buckets.go` | exact |
| `MFA/admin_settings.go`, `MFA/admin_permissions.go` | config | — | `SOC/admin_settings.go`, `SOC/admin_permissions.go` | exact |
| `MFA/config.go`, `MFA/config/config.yaml` | config | — | `SOC/config.go`, `SOC/config/config.yaml` | exact |
| `MFA/mail.go` + `MFA/views/mail/two_factor_code{,-en}.htm` | config + template | — | `sm-paymentgateway-plugin/mail.go`; template `USER/views/mail/restore-en.htm` | exact |
| `MFA/boot.go` (listeners: `PasswordChangedEvent`, `DeletingEvent`) | event listener | event-driven | `sm-chatbots-plugin/boot.go` :82-98 | exact |
| `MFA/classes/gate.go` (implements `userclasses.LoginGate`) | service | request-response | Research Code Example 1 + `SOC/plugin.go` :101 publish | role-match |
| `MFA/classes/challenge.go` (create, guarded UPDATEs) | service | CRUD | `USER/classes/api_tokens.go` (hash), Research Code Example 2 | role-match |
| `MFA/classes/totp.go`, `qr.go` | utility | transform | Research Code Examples 3, 4 | no analog (new lib) |
| `MFA/classes/webauthn.go` | service | request-response | Research Code Examples 5, 6; cooler use from `SOC/classes/codes.go` :171-215 | partial |
| `MFA/classes/recovery.go`, `email.go`, `trusted.go`, `events.go` | service | CRUD / event | `USER/classes/api_tokens.go`; `USER/classes/events.go` | role-match |
| `MFA/controllers/helpers.go` | utility | — | `SOC/controllers/helpers.go` :1-26 | exact |
| `MFA/controllers/challenge.go`, `management.go`, `passwordless.go` | controller | request-response | `USER/controllers/api_tokens.go` (jwt.auth deps), `SOC/controllers/complete.go` (unauth) | exact |
| `MFA/models/*.go` (5 tables + `Settings`), `MFA/models/registry.go` | model | CRUD | `USER/models/api_token.go`; `SOC/models/settings.go`; `SOC/models/registry.go` | exact |
| `MFA/models/settings/fields.yaml` | config | — | `SOC/models/settings/fields.yaml` | exact |
| `MFA/updates/*.go`, `MFA/updates/registry.go` | migration | — | `USER/updates/202610040001_create_api_tokens.go`; `SOC/updates/*` | exact |
| `MFA/lang/{en,pl}/lang.yaml` | config | — | `SOC/lang/*/lang.yaml` | exact |
| `MFA/go.mod`, `MFA/go.work` | config | — | `SOC/go.mod`, `SOC/go.work` | exact |
| `MFA/testmain_test.go`, `MFA/harness_test.go`, `*_test.go` | test | — | `SOC/testmain_test.go`, `SOC/harness_test.go` | exact |
| `MFA/README.md` | docs | — | `SOC/README.md` | exact |
### sociallogin, apps, frontend
| File | Role | Data Flow | Closest Analog | Match Quality |
|---|---|---|---|---|
| `SOC/classes/flow.go` (mod: gate in `signIn` :311-324) | service | request-response | itself + `USER` Login gate | exact |
| `SOC/classes/codes.go` (mod: `CompletionPayload.Challenge`) | model | — | itself :35-40 | exact |
| `SOC/controllers/complete.go` (mod: write challenge as 200) | controller | request-response | itself :17-35 | exact |
| `SOC/models/settings.go` + fields.yaml (or mfa settings: social toggle lives in **mfa** per D-12) | — | — | — | — |
| `DEMO/summer.yaml`, `DEMO/go.work`, `DEMO/.gitmodules`, `DEMO/plugins.gen.go`, `DEMO/config/golem15.mfa.yaml` | config | — | sociallogin entries in the same files | exact |
| `DEMO/mfa_test.go` (boot + routes resolve) | test | — | `DEMO/sociallogin_test.go` | exact |
| `BM/summer.yaml` etc. | config | — | sociallogin entry in `BM/summer.yaml` :6-7 | exact |
| `fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go` (drop `"/devices"`) | test | — | itself :79-112 | exact |
| `VDEMO/app/composables/useDemoAuth.ts` (mod: challenge-aware login + verify) | composable | request-response | itself | exact |
| `VDEMO/app/pages/login.vue` (mod: challenge step) | component | request-response | itself | exact |
| `VDEMO/app/pages/account/security.vue` (new: TOTP setup/confirm, codes, disable) | component | request-response | `VDEMO/app/pages/login.vue` (form) + `subscriptions.vue` (logged-in gate) | role-match |
| `VDEMO/app/composables/useTwoFactor.ts` (new) | composable | request-response | `VDEMO/app/composables/useDemoAuth.ts` | role-match |
| `VDEMO/app/layouts/default.vue` (mod: "Security" link) | component | — | itself :22-25 | exact |
---
## Pattern Assignments
### `USER/classes/login_gate.go` (service registry, request-response)
**Analog:** `USER/classes/oauth_providers.go` :13-31: a seam interface another plugin publishes, documented with the explicit-type-argument warning.
```go
// OAuthProviderSource is what a social-login plugin publishes so the user
// plugin can list its providers ... The user plugin looks it up on every
// request, so a plugin that boots later is still found.
//
// Publish it with an explicit type argument,
//
// app.Publish[classes.OAuthProviderSource](src)
//
// because a bare app.Publish(src) keys the service on its concrete type and
// the lookup misses it (the list then stays empty).
type OAuthProviderSource interface { ... }
```
Lookup side (`USER/controllers/api_controller.go` :236-242):
```go
func oauthProviderSource(app *backpack.App) (classes.OAuthProviderSource, bool) {
if app == nil {
return nil, false
}
src, ok := app.Lookup[classes.OAuthProviderSource]()
return src, ok && src != nil
}
```
Registry body: copy Research Code Example 1 (`LoginGates` with `sync.RWMutex`, `Add`, `Check` fail-closed). The user plugin publishes `*classes.LoginGates` in `Register`; mfa calls `.Add` in `Boot`. `TwoFactorAdmin` (D-15) is a second interface in the same style, published by mfa with `app.Publish[userclasses.TwoFactorAdmin](…)`.
---
### `USER/classes/events.go` + `USER/controllers/registration.go` (events, event-driven)
**Analog event type** (`classes/events.go` :59-67), copy for `PasswordChangedEvent{User, Tx, Source}` and `LoginEvent{User, Source}`; add `LoginEventName`/`PasswordChangedEventName` constants like :8-15:
```go
// DeactivatingEvent fires after a user is soft-deleted ... inside the same
// transaction ... A listener error rolls the deactivation back.
type DeactivatingEvent struct {
User *models.User
Tx *gorm.DB
}
```
**Fire helper** (`controllers/registration.go` :166-171), copy as `FirePasswordChangedEvent` / `FireLoginEvent` (exported so mfa and sociallogin can fire the login event after verify/passwordless):
```go
func FireDeactivatingEvent(ctx context.Context, app *backpack.App, user *models.User, tx *gorm.DB) error {
if app == nil || app.Events == nil {
return nil
}
return app.Events.Fire(ctx, &classes.DeactivatingEvent{User: user, Tx: tx})
}
```
---
### `USER/controllers/api_controller.go` (controller, request-response)
**Login** :47-127. Insert the gate after `CheckAndRecordLogin(…, true)` (:94-101) and before `mintFor` (:115). `fields` is already parsed at :53 (`readFields(r)`), so pass it into `LoginAttempt` (Pitfall 2). Per D-18 A10, **move** the soft-delete restore block (:87-93) after the gate:
```go
if user.DeletedAt.Valid {
if err := gdb.WithContext(ctx).Unscoped().Model(user).Update("deleted_at", nil).Error; err != nil {
writeOpaque500(w)
return
}
sendTemplateVars(ctx, app, mailName(ctx, user, "golem15.user::mail.reactivate"), user.Email, mailVars(displayName(user), "", ""))
}
if err := classes.CheckAndRecordLogin(ctx, gdb, user, ip, true); err != nil {
...
}
// <- gate goes here (Research Code Example 1), then the restore block, rehash, TouchLastSeen, mintFor
token, err := mintFor(app, secret, user, requestURL(r))
...
writeJSON(w, http.StatusOK, map[string]any{"token": token, "user": payload})
```
Error idiom everywhere: `writeOpaque500(w); return`. Fire `LoginEvent` just before the final `writeJSON`.
**Refresh** :179-213. After `bouncer.Refresh` succeeds, add the JTI hand-over (Research Code Example 8) next to the existing best-effort `TouchLastSeen` block, with the same "log, never fail" idiom:
```go
if sub, _, _, _, err := bouncer.VerifyClaims(next, secret); err == nil {
if id, err := strconv.ParseUint(sub, 10, 64); err == nil && id != 0 {
if err := classes.TouchLastSeen(r.Context(), gdb, uint(id)); err != nil {
slog.Default().Warn("golem15.user: stamp last_seen on refresh", "user_id", id, "err", err)
}
}
}
```
`bouncer.VerifyClaims(tokenString, secret) (sub, iat, exp, jti, err)` at `summercms.go/modules/bouncer/jwt.go:194`; `bouncer.Mint(secret, sub, issuerURL, ttl) (token, jti, err)` at `bouncer/mint.go:31`.
**Password-write sites** for `FirePasswordChangedEvent`: ResetPassword :335-343 (`Updates(map[string]any{"password": hashed, "tokens_valid_after": now, "email_verified_at": now})`), ChangePassword :589-597. Wrap the `Updates` + fire in `gdb.Transaction` so a listener error rolls back (Pattern 5). Do **not** fire from the rehash at :102-110.
**Blacklist on revoke** (copy from Logout :137-157):
```go
if bl, ok := app.Lookup[bouncer.BlacklistStore](); ok && bl != nil {
if err := bl.Add(r.Context(), jti, exp, time.Now()); err != nil {
writeOpaque500(w)
return
}
}
```
**Mail send helper** :931-943 (`sendTemplateVars`): mfa copies it verbatim with its own log prefix.
---
### `USER/controllers/users_admin_controller.go` + `config_form.yaml` + `admin_permissions.go` (cabana, CRUD)
**Analog:** `AdminRecordActions` :662-720. Add one more entry built like the `action(...)` closure but with `Permissions: []string{PermissionResetTwoFactor}`, `Danger: true`, `Applies` = lookup `classes.TwoFactorAdmin` (false when absent) then `HasTwoFactor`, `Run` = `cabana.TxFromContext(ctx)` then `svc.Reset(ctx, tx, user)`:
```go
return pact.AdminRecordAction{
Name: name,
Label: usersLang + label,
Confirm: usersLang + confirm,
Permissions: []string{PermissionAccessUsers},
Applies: func(ctx context.Context, record any) (bool, error) { ... },
Run: func(ctx context.Context, in pact.AdminRecordActionInput) (pact.AdminRecordActionResult, error) {
user, err := asUser(in.Record)
...
tx, ok := cabana.TxFromContext(ctx)
if !ok {
return pact.AdminRecordActionResult{}, errors.New("users admin: a record action needs its transaction")
}
...
return pact.AdminRecordActionResult{Message: usersLang + success}, nil
},
}
```
`config_form.yaml` :17 becomes `recordActions: [activate, unban, unsuspend, reset_two_factor]`. Permission entry copies `golem15.users.manage_privileged_groups` in `admin_permissions.go` (no `Roles`). Lang keys go in `USER/lang/*/lang.yaml`.
Admin password change :238-247: `changed, err := c.storePassword(ctx, user)` → when `changed`, fire `PasswordChangedEvent` with `requestDB(ctx, c.db)` as `Tx`.
---
### `USER/controllers/devices.go` (controller, request-response + CRUD)
**Analog:** `USER/controllers/api_tokens.go`.
**Imports** (:3-17):
```go
import (
"errors"
"net/http"
"strconv"
"strings"
"time"
"git.golem15.com/golem15/sm-user-plugin/classes"
"git.golem15.com/golem15/sm-user-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/golem15/summercms/modules/wire"
"gorm.io/gorm"
)
```
**Auth deps** (:113-129), reuse for `authorize`, `GET devices`, `DELETE devices/{id}` (routes carry `"jwt.auth"`):
```go
func apiTokenDeps(w http.ResponseWriter, r *http.Request, app *backpack.App) (*gorm.DB, *bouncer.Principal, bool) {
...
db, ok := app.Lookup[*gorm.DB]()
...
user, ok := bouncer.User(r.Context())
if !ok || user == nil || user.ID == 0 {
writeUnauthorized(w)
return nil, nil, false
}
return db, user, true
}
```
**List** (:66-85) uses `wire.Slice(data)` for an empty `[]`. **Destroy by id** (:88-111): `strconv.ParseUint(r.PathValue("id"), 10, 64)`, 404 on `gorm.ErrRecordNotFound`, scoped `Where("user_id = ?", user.ID)`. Device `initiate`/`status` are unauthenticated; status uses the guarded `confirmed→used` UPDATE (Research Code Example 7) and mints with `bouncer.Mint` to keep the JTI (`mintFor` discards it).
### `USER/routes.go` (route)
Add inside the existing `/_user/api/v1` group (:17-37). Keep each `g.Where("id", "[0-9]+")` **directly after** its `{id}` route (Pitfall 17), as at :35-36:
```go
g.Delete("/tokens/{id}", controllers.APITokenDestroy(p.app), "jwt.auth")
g.Where("id", "[0-9]+")
```
### `USER/models/device_auth_session.go` (model)
**Analog:** `USER/models/api_token.go` :10-50: explicit `gorm:"column:…"` tags, `TokenHash string … json:"-"`, `TableName()`, `Fillable()`, `Hidden()`, `func init() { Register(APIToken{}) }`. Table name `golem15_user_device_auth_sessions`; columns token_hash, short_code, status, user_id, device_name (bounded, Pitfall 14), ip, `jwt_jti`, expires_at, confirmed_at, used_at, last_activity_at.
### `USER/updates/2026102X0001_create_device_auth_sessions.go` (migration)
**Analog:** `USER/updates/202610040001_create_api_tokens.go` :8-36:
```go
var apiTokenMigrations = []*gormigrate.Migration{
{
ID: "202610040001_create_user_api_tokens",
Migrate: func(tx *gorm.DB) error {
return tx.Exec(`CREATE TABLE user_api_tokens (
id BIGSERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
...
token_hash VARCHAR(64) NOT NULL UNIQUE,
...
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX user_api_tokens_user_id_index ON user_api_tokens (user_id);`).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("DROP TABLE IF EXISTS user_api_tokens").Error
},
},
}
func init() { Register(apiTokenMigrations...) }
```
`user_id` is nullable for device sessions (pending rows have no user).
### `USER/classes/device_auth.go` (service)
**Analog:** `USER/classes/api_tokens.go` :75-111: `crypto/rand` bytes, hash with `HashAPIToken` (SHA-256 hex = the D-06 `sha256Hex` scheme), never store plaintext:
```go
raw := make([]byte, 32)
if _, err := rand.Read(raw); err != nil {
return "", nil, err
}
...
func HashAPIToken(secret string) string {
sum := sha256.Sum256([]byte(secret))
return hex.EncodeToString(sum[:])
}
```
Reuse `HashAPIToken` (or an exported alias) for device, challenge and trusted-device tokens instead of a new hash func.
---
### `MFA/plugin.go` (plugin entry)
**Analog:** `SOC/plugin.go`. Copy package doc, the interface assertion block (:26-35, add `pact.HasMailTemplates`, `surf.BucketProvider`), `PluginID` const, `//go:embed config` / `lang`, `Requires() []string{"golem15.user"}` (:72), and the Boot shape that **needs no database** (:91-102; DEMO `boot_test.go` boots without a DB, Pitfall 20):
```go
func (p *Plugin) Boot(app *backpack.App) error {
p.app = app
copyLegacyEnv(app)
if _, err := LoadConfig(app); err != nil {
return err
}
return app.Publish[userclasses.OAuthProviderSource](&providerSource{app: app, reg: p.reg})
}
...
func (p *Plugin) Migrations() []*gormigrate.Migration { return updates.All() }
func (p *Plugin) Models() []any { return models.All() }
func init() { party.Register(&Plugin{}) }
```
mfa's Boot: look up `*userclasses.LoginGates` and `Add` the gate, `app.Publish[userclasses.TwoFactorAdmin](…)`, register listeners. DB, cache and mailer are looked up per request.
### `MFA/routes.go` (route)
**Analog:** `SOC/routes.go` :12-26:
```go
func (p *Plugin) Routes(r pact.Router) error {
r.Group("/_user/api/v1", surf.Use("throttle:user-api"), func(g pact.Router) {
g.Get("/oauth-complete", controllers.Complete(p.app))
...
})
return nil
}
```
mfa: two groups on `/_user/api/v1/2fa`: challenge + passwordless with `surf.Use("throttle:2fa-verify")`; management routes with `"jwt.auth"` per route (as `USER/routes.go` :33-36) and `g.Where("id", "[0-9]+")` after each `{id}` route.
### `MFA/buckets.go` (`2fa-verify`, 10/min per IP)
**Analog:** `sm-oauthserver-plugin/buckets.go` :29-111, the per-IP variant at :96-99:
```go
var _ surf.BucketProvider = (*Plugin)(nil)
func (p *Plugin) Buckets() map[string]surf.Bucket {
trusted := surf.TrustedProxies(nil)
if p.app != nil {
trusted = surf.TrustedProxies(p.app.Config)
}
ip := func(r *http.Request) string { return surf.ClientIP(r, trusted) }
...
BucketMCPRegister: {
Name: BucketMCPRegister, Max: 10, Decay: time.Minute,
Key: func(r *http.Request) string { return "oauth-mcp-register-ip:" + ip(r) },
},
```
Bucket names are global; mfa owns `"2fa-verify"`.
### `MFA/admin_settings.go`, `MFA/admin_permissions.go`, `MFA/models/settings.go`, `MFA/models/settings/fields.yaml`, settings migration
**Analog:** `SOC/admin_settings.go` (whole file), `SOC/admin_permissions.go` (whole file), `SOC/models/settings.go`, `SOC/updates/202610200001_create_golem15_sociallogin_settings.go`.
```go
func (p *Plugin) Settings() []pact.SettingsItem {
return []pact.SettingsItem{{
Code: SettingsCode,
Label: models.LangPrefix + "settings.label",
...
Permissions: []string{PermissionAccessSettings},
Form: "models/settings/fields.yaml",
NewModel: func() any { return &models.Settings{} },
}}
}
```
Settings model essentials (`SOC/models/settings.go`): singleton `ID` 1, nullable pointer columns where NULL = config default, `TableName`, `Fillable`, `Rules` (`"nullable|max:255"` style), `BeforeValidate` returning `&cabana.ValidationError{Details: map[string]any{"field": []string{"…"}}}` (:84-91; use it for the methods/groups allow-lists), and `LoadSettings(ctx, db)` per request (:129-141):
```go
func LoadSettings(ctx context.Context, db *gorm.DB) (*Settings, error) {
...
var rows []Settings
if err := db.WithContext(ctx).Where("id = ?", 1).Limit(1).Find(&rows).Error; err != nil {
return nil, fmt.Errorf("golem15.sociallogin: settings: %w", err)
}
if len(rows) == 0 {
return &Settings{ID: 1}, nil
}
return &rows[0], nil
}
```
Migration seeds row 1: `INSERT INTO … (id, created_at, updated_at) VALUES (1, NOW(), NOW())`, executed through `run(tx, []string{…})` from `SOC/updates/registry.go`. Field types: `dropdown` (mode), `taglist` `mode: array` (methods, groups), `number`, `switch` (Research §Q8). `TOTP secret` column uses `lagoon.Encrypted` with `gorm:"…;type:text" json:"-"` like `SOC/models/settings.go` :39.
### `MFA/models/*.go` + `MFA/updates/*.go` (5 tables)
Model: `USER/models/api_token.go` (see above). Registry files: `SOC/models/registry.go` and `SOC/updates/registry.go` (whole files, `Register`/`All`, `run` helper). Migration SQL: `USER/updates/202610040001_create_api_tokens.go` style with `REFERENCES users(id) ON DELETE CASCADE`. Table names keep PHP's `golem15_user_*` (D-16).
### `MFA/mail.go` + `MFA/views/mail/two_factor_code{,-en}.htm`
**Analog:** `sm-paymentgateway-plugin/mail.go` :1-24:
```go
//go:embed views/mail
var mailFS embed.FS
func (p *Plugin) MailTemplatesFS() fs.FS { return mailFS }
func (p *Plugin) MailTemplates() []string {
return []string{
"golem15.paymentgateway::mail.order.canceled",
"golem15.paymentgateway::mail.order.canceled-en",
```
mfa lists `golem15.mfa::mail.two_factor_code` and `…-en` (postcard requires the owning plugin prefix, Pitfall 18). Template front-matter from `USER/views/mail/restore-en.htm`:
```
subject = "Reset your password"
description = "Password reset"
layout = "user"
==
Hello **{{ .Name }}**.
```
mfa cannot use the `user` layout alias; use the default layout or ship `views/mail/layouts/…` with its own `MailLayouts()`. Pass both cases of vars (`"name"`/`"Name"`, `"code"`/`"Code"`, `"expiry_minutes"`/`"ExpiryMinutes"`) as `USER mailVars` (:924-929) does. Locale: `userclasses.MailTemplate(base, userclasses.ResolveMailLocale(ctx, user))`.
### `MFA/boot.go` listeners
**Analog:** `sm-chatbots-plugin/boot.go` :82-98:
```go
app.Events.Listen[*userclasses.DeletingEvent](PluginID, p.onUserDeleting)
...
func (p *Plugin) onUserDeleting(ctx context.Context, ev *userclasses.DeletingEvent) error {
if p == nil || ev == nil || ev.User == nil {
return nil
}
db := ev.Tx
if db == nil {
db = p.db
}
...
return db.WithContext(ctx).Where("user_id = ?", ev.User.ID).Delete(&models.BotFlag{}).Error
}
```
mfa: `PasswordChangedEvent` → delete trusted devices through `ev.Tx`.
### `MFA/controllers/*.go` (controller, request-response)
- **Helpers:** copy `SOC/controllers/helpers.go` :22-26 (`writeJSON` with `Cache-Control: no-cache, private` + `wire.WriteJSON`).
- **Management (jwt.auth):** `USER/controllers/api_tokens.go` `apiTokenDeps` pattern (above). Management errors are PHP's 401 `{"error":"Unauthorized"}`.
- **Challenge (unauth):** shape of `SOC/controllers/complete.go` :17-35: read input, call a classes function, log with `slog.Default().Error("golem15.mfa: …", "err", err)` + 500 `{"error":"Server error"}`, map not-found to the PHP status.
- **Token mint after verify:** `usercontrollers.FinishLogin` → `usercontrollers.IssueToken` → `usercontrollers.APIArray` exactly as `SOC/classes/flow.go` :311-324 (`signIn`), then `FireLoginEvent`.
- **Validation:** 422 `{error, errors}` via a local `writeValidation` copied from `USER/controllers/api_controller.go` :945-950.
### `MFA/classes/webauthn.go` ceremony state in cooler
**Analog:** `SOC/classes/codes.go` :171-215 (`cooler.FromApp(app)`, `cache.Set(ctx, key, value, ttl)`, `cache.Pull(ctx, key)`, reject `len(code) > 128`). Ceremony state needs no AEAD sealing (it holds no secret), so a plain JSON `Set`/`Pull` suffices.
### `MFA/testmain_test.go`, `MFA/harness_test.go`
**Analog:** `SOC/testmain_test.go` (TestMain with `flag.Parse()` + `testing.Short()`, `postgres:16-alpine`, `freshDB(t)` at :81-83) and `SOC/harness_test.go` :34-80 (`bootSocialWith`: freshDB → `lagoon.Use` → `httptest.NewUnstartedServer` → temp config dir with `app.yaml` → `party.Activate`). Rename DB/user to `golem15_mfa`. Blank-import `_ "git.golem15.com/golem15/sm-user-plugin"` as SOC does (:22).
### `MFA/go.mod`, `MFA/go.work`
**Analog:** `SOC/go.work`:
```
go 1.27.0
use .
replace git.golem15.com/golem15/summercms => ../summercms.go
replace git.golem15.com/golem15/sm-user-plugin => ../fonoteka.go/plugins/golem15/user
```
`SOC/go.mod` requires `sm-user-plugin v0.1.5` and `summercms v0.3.1`; mfa requires the **new** user-plugin tag carrying the seams, plus pquerna/otp v1.5.0, go-webauthn v0.18.2, boombuler/barcode v1.1.0 (after the decision note).
---
### `SOC/classes/flow.go`, `SOC/classes/codes.go`, `SOC/controllers/complete.go` (D-09)
`signIn` (:311-324) is where the gate goes, before `IssueToken`, with `Source: "social"`, and only when the mfa social toggle is on (the gate itself reads the setting; sociallogin just calls `gates.Check`). `linkIdentity` (flow.go ~:128-165) stays ungated.
```go
func signIn(ctx context.Context, app *backpack.App, r *http.Request, db *gorm.DB, user *usermodels.User, action, returnTo string) (Outcome, error) {
if err := usercontrollers.FinishLogin(ctx, app, db, user, ClientIP(app, r)); err != nil {
return Outcome{}, err
}
token, err := usercontrollers.IssueToken(app, user, r)
...
return Outcome{Kind: OutcomeLogin, Token: token, User: payload, Action: action, ReturnTo: returnTo}, nil
}
```
Note: `FinishLogin` restores soft-deleted accounts, so per D-18 A10 the gate must run **before** `FinishLogin`. Add `Challenge map[string]any \`json:"challenge,omitempty"\`` to `CompletionPayload` (codes.go :35-40); in `Complete` (:34), when `p.Challenge != nil`, `writeJSON(w, http.StatusOK, p.Challenge)` instead of `writeSession`.
### DEMO / BM adoption
- `DEMO/summer.yaml`: add `- id: golem15.mfa` / `module: git.golem15.com/golem15/sm-mfa-plugin` after `golem15.sociallogin` (pattern: existing entries).
- `DEMO/go.work`: add `./plugins/golem15/mfa` to `use (…)`.
- `DEMO/.gitmodules`: copy the `plugins/golem15/user` stanza (`url = git@git.golem15.com:golem15/sm-mfa-plugin.git`, `branch = master`); add via `ssu`.
- Regenerate `plugins.gen.go`/`main.go` with `summer build`.
- `DEMO/config/golem15.mfa.yaml`: WebAuthn `rp_id`/`origins` override for local use (Pitfall 7; app url is `http://127.0.0.1:8097`, frontend `http://127.0.0.1:3020`).
- Test: copy `DEMO/sociallogin_test.go` :16-40 (`testConfig` → `backpack.New` → `party.Activate(app, PluginIDs)` → `surf.BuildRouter` → assert `/_user/api/v1/2fa/...` and `/devices` routes resolve).
- `BM/summer.yaml` :4-7 same entry; bm stays API-only.
### `fonoteka.go/plugins/golem15/fonoteka/phase07_coverage_test.go`
Remove `"/devices",` from `banned` (:97-105); keep `"2fa"`. Separate pointer-bump commit in fonoteka.go.
### `VDEMO` (D-20)
**`app/composables/useDemoAuth.ts`** (whole file, 20 lines). Extend `login` to detect `two_factor_required` and return the challenge instead of throwing; add `verify(challengeToken, method, code)` → `POST /_user/api/v1/2fa/verify` and `recovery(challengeToken, code)` → `/2fa/recovery`, both setting `token.value`:
```ts
export function useDemoAuth() {
const token = useCookie<string | null>(DEMO_TOKEN_COOKIE, { maxAge: 60 * 60 * 24, sameSite: 'lax' })
const loggedIn = computed(() => !!token.value)
async function login(email: string, password: string) {
const res = await $fetch<{ token?: string; ... }>(
'/_user/api/v1/login',
{ method: 'POST', body: { email, password } },
)
const value = res.token ?? res.access_token ?? res.data?.token ?? res.data?.access_token
if (!value) throw new Error('No token in the login answer')
token.value = value
}
```
**`app/pages/login.vue`**: keep the `submit`/`busy`/`error` idiom and the `next` redirect guard (:15-22); add a second form shown when a challenge is pending (code input with `autocomplete="one-time-code"`, a "use a recovery code" toggle). Error extraction `e?.data?.message || e?.data?.error`.
**New `app/pages/account/security.vue`** + **`app/composables/useTwoFactor.ts`**: `$fetch` with `headers: { Authorization: \`Bearer ${token.value}\` }` to `/2fa/status`, `/2fa/totp/setup` (render `qr_code_svg` with `v-html`, the SVG is server-generated), `/2fa/totp/confirm` (show `recovery_codes` from D-19 once), `DELETE /2fa/totp`. Logged-out notice copied from `subscriptions.vue` :21-23. Markup classes: `wrap section section--narrow`, `panel`, `form`, `button`, `form__error`, `notice`.
**`app/layouts/default.vue`** :22-25: add a `NuxtLink to="/account/security"` next to "Log out". `/_user` is already in `goPaths` in `nuxt.config.ts`, so no proxy change.
---
## Shared Patterns
### JSON responses
**Source:** `SOC/controllers/helpers.go` :22-26; user plugin `writeJSON`/`writeOpaque500`/`writeUnauthorized`.
**Apply to:** every mfa and device handler. Timestamps through `wire.Time` (`+00:00`), lists through `wire.Slice`.
### Cross-plugin service seams
**Source:** `USER/classes/oauth_providers.go` :13-31 and `SOC/plugin.go` :101. Always `app.Publish[Iface](impl)` with the explicit type and look up per request. **Apply to:** `LoginGates`, `TwoFactorAdmin`.
### Typed events
**Source:** `USER/classes/events.go`, `USER/controllers/registration.go` :129-171, listener `sm-chatbots-plugin/boot.go` :82-98. Events carry `Tx`; listener errors roll the write back. **Apply to:** `LoginEvent`, `PasswordChangedEvent`, all `golem15.user.2fa.*` events (as typed structs with `…EventName` constants).
### Hashed tokens at rest
**Source:** `USER/classes/api_tokens.go` :75-99 (`crypto/rand` 32 bytes, `HashAPIToken` SHA-256 hex, `token_hash VARCHAR(64) NOT NULL UNIQUE`). **Apply to:** challenge, trusted-device and device-session tokens. Slow hash for recovery/email codes: `bouncer.HashPassword(cost, plain)` / `bouncer.CheckPassword(hash, plain)` with the cost read as in `USER/controllers/api_controller.go` :102-105.
### Guarded single-statement state changes
**Source:** Research Code Examples 2 and 7 (no existing analog; the user plugin uses read-modify-write). **Apply to:** challenge attempts/completion, recovery consume, device confirm/used, email send count.
### Raw-SQL migrations with registry
**Source:** `SOC/updates/registry.go`, `USER/updates/202610040001_create_api_tokens.go`. **Apply to:** all six tables. Never GORM AutoMigrate.
### Docker-gated tests
**Source:** `SOC/testmain_test.go` (flag.Parse + testing.Short). **Apply to:** mfa and user-plugin DB tests.
## No Analog Found
| File | Role | Data Flow | Reason |
|---|---|---|---|
| `MFA/classes/totp.go`, `MFA/classes/qr.go` | utility | transform | First use of pquerna/otp and boombuler; use Research Code Examples 3 and 4 |
| `MFA/classes/webauthn.go` (ceremony logic) | service | request-response | First WebAuthn use; use Research §Q6 and Code Examples 5-6 (only the cooler storage has an analog) |
## Metadata
**Analog search scope:** `fonoteka.go/plugins/golem15/user`, `sm-sociallogin-plugin`, `sm-oauthserver-plugin`, `sm-paymentgateway-plugin`, `sm-chatbots-plugin`, `sm-demo-app`, `sm-bm-app`, `vue-demo-app`, `fonoteka.go/plugins/golem15/fonoteka`, `summercms.go/modules/bouncer`
**Files scanned:** ~45
**Pattern extraction date:** 2026-10-21

View File

@@ -0,0 +1,77 @@
---
phase: "19.6"
slug: "two-factor-auth-and-device-flow-in-sm-user-plugin"
# status lifecycle: draft (seeded by plan-phase) → validated (set by validate-phase §6)
# audit-milestone §5.5 distinguishes NOT-VALIDATED (draft) from PARTIAL (validated + nyquist_compliant: false) (#2117)
status: draft
nyquist_compliant: false
wave_0_complete: false
created: "2026-10-21"
---
# Phase 19.6 — Validation Strategy
> Per-phase validation contract for feedback sampling during execution.
---
## Test Infrastructure
| Property | Value |
|----------|-------|
| **Framework** | Go `testing` + `httptest`; testcontainers-go Postgres (DB tests skip under `-short`) |
| **Config file** | none — Wave 0 adds `sm-mfa-plugin/testmain_test.go` and `harness_test.go` (copied from `sm-sociallogin-plugin`) |
| **Quick run command** | `go -C ../sm-mfa-plugin test -short ./...` and `go -C ../fonoteka.go test -short ./plugins/golem15/user/...` |
| **Full suite command** | `scripts/check-phase19.6.sh` (go, userplugin, mfa, sociallogin, demo, bm, fonoteka, docker, docs stages) |
| **Estimated runtime** | ~180 seconds (full, with Docker) |
---
## Sampling Rate
- **After every task commit:** `go vet ./...` + `go test -short ./...` of the touched repo
- **After every plan wave:** full suites (Docker) of every touched repo
- **Before `/msd-verify-work`:** `scripts/check-phase19.6.sh` must be green
- **Max feedback latency:** 60 seconds for the short loop
---
## Per-Task Verification Map
Decision → test map is in `19.6-RESEARCH.md` § Validation Architecture; the planner assigns task IDs.
| Task ID | Plan | Wave | Requirement | Threat Ref | Secure Behavior | Test Type | Automated Command | File Exists | Status |
|---------|------|------|-------------|------------|-----------------|-----------|-------------------|-------------|--------|
| 19.6-XX-XX | TBD | TBD | D-XX | T-19.6-XX | per RESEARCH map | unit/integration | per RESEARCH map | ❌ W0 | ⬜ pending |
*Status: ⬜ pending · ✅ green · ❌ red · ⚠️ flaky*
---
## Wave 0 Requirements
- [ ] `sm-mfa-plugin/testmain_test.go`, `harness_test.go` — boot `golem15.user` + `golem15.mfa`
- [ ] WebAuthn fixtures — recorded attestation/assertion JSON or a virtual authenticator
- [ ] Clock injection for TOTP tests
- [ ] user-plugin harness helper for device-flow tests
---
## Manual-Only Verifications
| Behavior | Requirement | Why Manual | Test Instructions |
|----------|-------------|------------|-------------------|
| WebAuthn registration and login in a real browser | D-08 / SC-1 | Browser authenticator ceremony cannot run in `go test` | Register a passkey on the demo instance over HTTPS with a hostname RP ID, then sign in with it |
---
## Validation Sign-Off
- [ ] All tasks have `<automated>` verify or Wave 0 dependencies
- [ ] Sampling continuity: no 3 consecutive tasks without automated verify
- [ ] Wave 0 covers all MISSING references
- [ ] No watch-mode flags
- [ ] Feedback latency < 60s
- [ ] `nyquist_compliant: true` set in frontmatter
**Approval:** pending

View File

@@ -0,0 +1,460 @@
---
phase: 19.6.1-backend-administrators-screen
plan: 01
type: execute
wave: 1
depends_on: []
files_modified:
- modules/pact/capabilities.go
- modules/pact/README.md
- modules/cabana/action_extensions.go
- modules/cabana/backend_screens.go
- modules/cabana/backend_users_admin.go
- modules/cabana/backend_roles_admin.go
- modules/cabana/backend_privileges.go
- modules/cabana/backendscreens/controllers/users/config_list.yaml
- modules/cabana/backendscreens/controllers/users/config_form.yaml
- modules/cabana/backendscreens/controllers/users/config_filter.yaml
- modules/cabana/backendscreens/controllers/users/_hint.htm
- modules/cabana/backendscreens/models/user/columns.yaml
- modules/cabana/backendscreens/models/user/fields.yaml
- modules/cabana/backendscreens/controllers/roles/config_list.yaml
- modules/cabana/backendscreens/controllers/roles/config_form.yaml
- modules/cabana/backendscreens/models/userrole/columns.yaml
- modules/cabana/backendscreens/models/userrole/fields.yaml
- modules/cabana/http.go
- modules/cabana/registry.go
- modules/cabana/contracts.go
- modules/cabana/crud.go
- modules/cabana/backend_screens_test.go
- modules/cabana/backend_screens_smoke_test.go
- modules/cabana/README.md
- modules/phrasebook/backend/lang/en/lang.yaml
- modules/phrasebook/backend/lang/pl/lang.yaml
- docs/backend/admin-controllers.md
- docs/backend/users-and-permissions.md
autonomous: true
requirements: [SC-1, SC-2, SC-3, D-01, D-02, D-03, D-04, D-05, D-06, D-07, D-08, D-09, D-10, D-11, D-12, D-13, D-14, D-15, D-16, D-17, D-18, D-19, D-20, D-22]
estimate:
tokens: 330000
raw_tokens: 330000
tasks: 4
confidence: low
must_haves:
truths:
- "D-01, D-02, D-04: a superuser sees an Administrators tile (order 100) and a Roles tile (order 110) under Settings → System; the permission catalog gains backend.manage_users and backend.manage_roles granted to no role in code, so after an upgrade no developer or publisher admin sees a tile and the controller routes answer them 403."
- "D-14, D-15, SC-3: a plugin implementing pact.HasAdminActionExtensions adds record actions to any admin controller by ID, summercms.cabana.users included; they are offered on that controller's preview after the owner's actions, only to admins holding the target's RequiredPermissions and the action's Permissions and for whom Applies is true, and run through the existing row-locked RecordAction transaction; an unknown controller ID, a target without a preview block, a reserved or duplicate name, a nil Run, an empty label or an unknown permission stops boot."
- "An application whose plugins register no admin controllers still activates no admin: Activate returns nil routes without reading admin.jwt.secret, so examples/hello and the modules/surf admin-prefix tests pass unchanged."
- "D-03: for a non-superuser the Administrators list, search and filters never return a superuser, GET of a superuser id (show, preview partial, record action) answers 404, and the is_superuser field, column and filter are absent from the form and list schemas; a superuser sees all three."
- "D-05: a non-superuser create or update body carrying is_superuser (true or false, field hidden or not) answers 403 with backend::lang.user.superuser_forbidden on is_superuser and writes nothing; a superuser grants and removes it."
- "D-06: the Administrators form has a Permissions tab with a radio (inherit / allow / deny) permission editor stored in backend_users.permissions and merged last by FindByID."
- "D-07: a non-superuser cannot grant more than they hold: catalog permissions they lack are Locked options (a changed locked value answers 403 and the stored value is kept), roles whose stored plus code-declared grants they do not cover are locked in the role picker (assigning one answers 403 with user.role_ceiling, keeping an unchanged above-ceiling role still saves), and on the Roles screen codes they lack are locked."
- "D-08: an admin editing their own record changes login, email, first and last name; a change of their own role, superuser flag, permissions or activation answers 403 user.self_privileged (the role is compared after assignment and the save rolls back), deleting yourself answers 403 user.self_delete and deactivating yourself 403 user.self_deactivate; there is no redirect to My Account."
- "D-09: the last active superuser cannot be demoted (update), deactivated (record or bulk action) or soft-deleted (form or bulk delete); every reducing path counts the other active superusers inside the write transaction after pg_advisory_xact_lock, which is taken after the row lock, so concurrent demotions of the only two superusers leave one active superuser without a deadlock; admin:* commands never clear the flag and stay unchanged (SC-1)."
- "D-10: Activate and Deactivate are record and bulk actions toggling is_activated, Delete is a soft delete through deleted_at, the trashed filter lists deleted admins carrying the Deleted row state, Restore brings them back, and no code path hard-deletes a backend user."
- "D-11: password and confirmation are required on create and optional on update (blank keeps the password), at least 4 characters and at most 72 bytes, bcrypt-hashed at admin.password.bcrypt_cost like admin:create; a login or email already held (case-insensitive, deleted admins included) answers 422 on that field, saying when a deleted administrator holds it."
- "D-12: deactivate, soft delete and a password set for another admin set tokens_valid_after to now+1s, so the target's earlier JWTs answer 401 on the next request; deactivate and delete also stop personal API tokens (FindByID refuses inactive and deleted users); restore and activate leave tokens_valid_after unchanged; a password set leaves personal API tokens working, exactly as admin:reset-password does, and the docs say so."
- "D-13: creating an administrator sends no mail and offers no invite option."
- "D-16: the Roles form has name, code (slug preset from name), description and a Permissions tab with a checkbox permission editor; changing the code of a system role (is_system, or a role code-declared grants attach to) answers 422 user.role.code_locked on code; name, description and stored grants stay editable within the D-07 ceiling."
- "D-17: deleting a role that any admin holds, soft-deleted admins included, answers 403 whose message names the role and the count; deleting a system role answers 403 user.role.delete_system; a refused row in a bulk delete rolls back the whole selection."
- "D-19: a non-superuser holding backend.manage_users gets 403 user.outranked when updating, deleting, or running a record or bulk action on a non-superuser admin whose effective grants (role JSON, code-declared role grants, user overrides) their own grants do not cover."
- "D-20: a password sent for your own record on the Administrators form answers 403 user.own_password on password; My Account, which checks the current password, stays the place to change it."
- "Pitfall 1: GET of a superuser's record returns data.is_superuser true to a superuser, and saving that form untouched keeps the flag."
- "D-18, D-22: this plan is the server side of the three-plan split; cabana and pact READMEs and docs/backend/admin-controllers.md and users-and-permissions.md change in the same commits as the API, and TestDocsTree plus summer docs:build --check pass."
- "UI: the Administrators list shows backend::lang.user.list_empty when a search or filter matches nothing (an unfiltered list always holds the viewer) and the Roles list shows user.role.list_empty (seeded system roles keep it non-empty unfiltered); the existing loading skeletons, list.load_failed and form.load_failed alerts (also the D-03 404) and empty-search copy apply unchanged."
- "UI: Administrators list columns are login, first name, last name, email, role (dash when empty), last sign-in (dash when never), superuser (superusers only) and an invisible created column; active, deactivated, deleted and deleted+deactivated rows coexist, each state a text badge in RowStateBadges order; 20 rows per page with the existing pagination and horizontal scroll."
- "UI: bulk Activate, Deactivate and Restore act on the selection (menu enabled only with a selection); one refused row rolls the whole selection back and the danger toast shows the refusal; every refusal that can come back from a delete or an action is a ForbiddenError with Message set (UI-SPEC C8)."
- "UI: the Administrators preview shows at most one status callout by precedence deleted (danger) > deactivated (warning) > own account (warning), then at most one of Activate / Deactivate / Restore, then contributed actions in boot merge order wrapping with the footer, then Edit administrator; Deactivate is not offered on your own record; an action's 403 is a danger toast with the server message, 409 the existing stale toast, 404 the load-failed state."
- "UI: the create form starts blank with password and confirmation required (422 field lines and count banner on a miss or mismatch); the Account tab pairs login/email, first/last name and password/confirmation, then a full-width role picker that shows above-ceiling roles locked; the Roles create form starts unchecked with codes the editor lacks shown as locked rows; long values scroll inside the inputs and the header title truncates."
- "UI: a non-superuser editor sees no Superuser switch, column or filter, locked permission rows for codes they lack and locked roles above their ceiling; a 403 save shows the forbidden banner with the server message plus field lines and keeps values; a 422 shows field lines, the count banner and activates the first invalid field's tab."
- "UI: each Settings tile is listed only to holders of its code or superusers, so an admin holding only backend.manage_roles sees only Roles; a Roles bulk delete containing a role still assigned or a system role refuses the whole selection with a danger toast naming that role."
artifacts:
- path: "modules/pact/capabilities.go"
provides: "AdminActionExtension and HasAdminActionExtensions"
contains: "type HasAdminActionExtensions interface"
- path: "modules/cabana/action_extensions.go"
provides: "boot-time merge of contributed record actions"
contains: "func compileActionExtensions("
- path: "modules/cabana/backend_screens.go"
provides: "internal summercms.cabana pseudo-plugin: controllers, permissions, settings links, AdminFS"
contains: "AdministratorsControllerID"
- path: "modules/cabana/backend_users_admin.go"
provides: "summercms.cabana.users controller with hooks, providers and actions"
contains: "FormAfterUpdate"
- path: "modules/cabana/backend_roles_admin.go"
provides: "summercms.cabana.roles controller"
contains: "FormBeforeDelete"
- path: "modules/cabana/backend_privileges.go"
provides: "actsAsSuperuser, effectiveGrants, grantsCovered, guardOutranked, guardLastSuperuser"
contains: "pg_advisory_xact_lock"
- path: "modules/cabana/backendscreens/models/user/fields.yaml"
provides: "Administrators form (Account and Permissions tabs)"
contains: "permissioneditor"
- path: "modules/cabana/backend_screens_smoke_test.go"
provides: "Postgres smoke tests of the four slices"
contains: "TestBackendScreensPostgresSmoke"
key_links:
- from: "modules/cabana/http.go"
to: "modules/cabana/action_extensions.go"
via: "Activate merges contributions after compiling controllers and before compileContributions validates permissions"
pattern: "compileActionExtensions\\(reg, "
- from: "modules/cabana/http.go"
to: "modules/cabana/backend_screens.go"
via: "Activate appends the pseudo-plugin only after the no-controllers early return"
pattern: "withBackendScreens\\("
- from: "modules/cabana/backend_users_admin.go"
to: "modules/cabana/backend_privileges.go"
via: "every reducing write path calls the last-superuser guard inside the write tx"
pattern: "guardLastSuperuser\\("
- from: "modules/cabana/crud.go"
to: "modules/cabana/backend_users_admin.go"
via: "projectFullRecord asks the unexported data extender so is_superuser loads its stored value"
pattern: "extendRecordData\\("
- from: "modules/cabana/http.go"
to: "modules/cabana/backend_users_admin.go"
via: "formSchema and listSchema drop per-request hidden fields, columns and filters"
pattern: "hiddenSchemaFields\\("
prohibitions:
- statement: "Existing cabana payloads (record, list, schema, settings, navigation, /auth/me, /account) keep their shape for every controller other than the two new ones, and admin:* commands keep their behaviour and output: modules/cabana/commands.go, token_commands.go and account.go are not modified."
status: unresolved
verification: judgment
- statement: "No code path hard-deletes a backend user; tokens, linked_by references and attachments keep valid foreign keys."
status: unresolved
verification: judgment
- statement: "Neither backend.manage_users nor backend.manage_roles is granted to the developer, publisher or any other role through code-declared pact.Permission roles."
status: unresolved
verification: judgment
- statement: "No invite or welcome mail, send-reset-link action, force-change-on-next-login, impersonation, user groups, per-row list action menu or top-level navigation item is added (CONTEXT Deferred Ideas, D-02, D-13, D-15)."
status: unresolved
verification: judgment
- statement: "Framework code, READMEs, docs and tests name no consuming application; no new dependency is added and YAML stays on goccy/go-yaml."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not in user-story format; MVP mode is off, no story is invented): cabana gets WinterCMS's Administrators screens, so backend admins and their roles are managed in the admin SPA instead of only through `admin:*` console commands, with the privilege rules on the server and a record-action seam other plugins (19.7's link action) use.
This plan's slice (plan 1 of 3, wave 1, D-22): the whole server side. The tracer wires the new `pact` seam, the internal `summercms.cabana` pseudo-plugin, the Settings tile and the Administrators list and preview end to end, with a contributed record action running on the preview. Task 2 adds the form and every save-path rule, Task 3 the lifecycle actions and the last-superuser lock, Task 4 the Roles screen. Plan 02 adds the SPA parts of D-21 (field `trigger`, per-record permission locks), Plan 03 the unit and Postgres tests.
## Spec-less probe fallback
Spec-less probe fallback skipped for edges: no requirement IDs (phase_req_ids null) and no SPEC.md; visible choice. Edge rows come from 19.6.1-UI-SPEC.md "UI Considerations" instead: the covered rows for the server-rendered screens are lifted into `must_haves.truths` above (lines starting "UI:"), the D-21 rows go to Plan 02, the five backstop rows to Plan 03, and the dismissed row (static note text has no loading state) is not lifted. Prohibitions were recalled by hand (fallback §B) and authored descriptor-less.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- IDs (CONTEXT discretion, RESEARCH A4): plugin `summercms.cabana`, controllers `summercms.cabana.users` and `summercms.cabana.roles` (SPA routes `/backend/summercms/cabana/...`); `summercms` is not a reserved first segment (registry.go:46). The migration history id `summercms.cabana` is untouched; no migration.
- The pseudo-plugin is appended only when some real plugin registers admin controllers (RESEARCH Pattern 1, Pitfall 3). A plugin that only contributes record actions does not switch the admin on; its contributions are compiled whenever the admin is active.
- The D-14 seam carries record actions only (CONTEXT discretion, RESEARCH A5). `AdminActionExtension` is a struct so bulk or toolbar fields can be added later without breaking implementers. A record action still carries no input beyond the record (RESEARCH Open Question 5, for 19.7 to design).
- 19.6 D-15 ("Reset 2FA" contributed by an MFA plugin to the user plugin's Users screen) can use the seam as is: that screen has a preview block; the contributed name must not collide with a name the owner registers (RESEARCH Pattern 2).
- D-09 lock strategy (CONTEXT discretion): one `pg_advisory_xact_lock` on a fixed key taken after the row lock, plus a count in the write transaction (RESEARCH Pattern 4; a `FOR UPDATE` on the superuser set would deadlock).
- D-09 reachability: through the screens a single request cannot remove the last active superuser, because only an active superuser sees superusers (D-03) and changes to your own flag, activation or row are refused first (D-08), so the acting superuser always remains. The guard closes the concurrent case (superuser A demotes, deactivates or deletes B while B does the same to A) and any later write path. The smoke tests therefore exercise the self refusals; Plan 03 proves every path with a race held on the advisory lock.
- D-07 wildcard rule (RESEARCH A3): a role's wildcard grant counts as covered only when the editor holds the same or a broader wildcard.
- D-12 and personal API tokens (RESEARCH Pitfall 7, Open Question 6): a password set for another admin bumps `tokens_valid_after` (JWTs stop) and leaves personal API tokens working, exactly like `admin:reset-password`; deactivate and delete stop both. Documented in docs/backend/users-and-permissions.md. Revoking personal tokens on a password set would widen the phase; the user confirmed this reading on 2026-10-22 and D-12 is amended in CONTEXT.
- D-17 refusal shape: the delete answers 403 `ForbiddenError` with a translated message naming the role and the count, not a 422 `ValidationError`, because the SPA delete toast shows only `error.message` (UI-SPEC C8 note). This deviates from CONTEXT's word "validation error"; the visible requirement (the toast names the count) holds without an SPA change.
- D-16 system-role code change answers 422 on `code` (UI-SPEC C8), a field error on a save, not a delete refusal.
- Roles list has no users-count column (RESEARCH Open Question 7; cabana columns have no count key).
- The `is_superuser` projection and the per-request schema drops use unexported in-package interfaces, so no `pact` surface and no payload change for other controllers.
- Lifecycle copy, filters and columns follow UI-SPEC C2-C5 exactly; all `backend::lang.user.*`, `user.role.*`, `permissions.*` and `settings.category_system` copy lands in Task 1 (en and pl); the D-21 framework copy lands with Plan 02.
## Source coverage (whole phase)
| Source | Item | Plan | Status |
|---|---|---|---|
| GOAL | Administrators and Roles managed in the SPA with server-side rules and a plugin seam | 01-03 | COVERED |
| SC-1 | list, create, edit, deactivate admins; edit roles with the permission editor; payloads and admin:* unchanged | 01 (T1-T4), 03 | COVERED |
| SC-2 | only a superuser grants superuser; no self-raise; last active superuser protected | 01 (T2, T3), 03 | COVERED |
| SC-3 | record-action seam usable by plugins; unit tests for the rules | 01 (T1), 03 | COVERED |
| RESEARCH | Activation caveat, protected keys, save order, Pitfalls 1-10, advisory lock, ceilings, settings links | 01 | COVERED |
| CONTEXT | D-01..D-20 | 01 | COVERED |
| CONTEXT | D-21 (trigger, record locks, OpenAPI, dist) | 02 | COVERED |
| CONTEXT | D-18 (tests last, docs with the code), D-22 (three plans) | 01, 02, 03 | COVERED |
| UI-SPEC | C1-C5, C8, copy | 01 | COVERED |
| UI-SPEC | C6, C7, framework copy | 02 | COVERED |
| UI-SPEC | backstop rows | 03 | COVERED |
## Artifacts this phase produces (this plan)
- pact: `type AdminActionExtension struct { Controller string; RecordActions []AdminRecordAction }`, `type HasAdminActionExtensions interface { AdminActionExtensions() []AdminActionExtension }`.
- cabana exported: constants `PermissionManageUsers` ("backend.manage_users"), `PermissionManageRoles` ("backend.manage_roles"), `AdministratorsControllerID` ("summercms.cabana.users"), `RolesControllerID` ("summercms.cabana.roles"); methods `BackendUser.Fillable`, `BackendUser.Rules`, `BackendUser.FilterScopes`, `BackendUser.FilterScope`, `BackendUserRole.Fillable`, `BackendUserRole.Rules`.
- cabana unexported: `backendScreens` pseudo-plugin and `withBackendScreens`, `compileActionExtensions`, `Registry.permissionOrder`, `CompiledController` contributor map, `usersAdmin`, `rolesAdmin`, `actsAsSuperuser`, `roleGrantSet`, `effectiveGrants`, `grantsCovered`, `guardOutranked`, `guardLastSuperuser`, `superuserLockKey`, `revocationCutoff`, `recordDataExtender` (`extendRecordData`), `schemaFieldHider` (`hiddenSchemaFields`), `BackendUser.priorRoleID` and `priorLoaded`, filter scope names `filterByRole` and `filterTrashed`.
- Admin screens: controllers `summercms.cabana.users` (list, form, preview with hint, filters, record and bulk actions activate / deactivate / restore) and `summercms.cabana.roles` (list, form); settings link entries `administrators` and `roles` in category `backend::lang.settings.category_system`.
- Embedded YAML and partial under `modules/cabana/backendscreens/`.
- Lang groups `backend::lang.user` (with `user.role`), `backend::lang.permissions`, key `backend::lang.settings.category_system` (en, pl).
- Smoke tests `TestBackendScreensSmoke` (-short), `TestBackendScreensPostgresSmoke`, `TestAdministratorsFormSmoke`, `TestAdministratorsLifecycleSmoke`, `TestRolesSmoke` (Postgres).
<objective>
Build the server side of the Administrators and Roles screens (D-22 plan 01): the D-14 `pact` action-extension seam, the internal `summercms.cabana` pseudo-plugin with both controllers, every privilege rule of D-03 to D-12, D-16, D-17, D-19 and D-20 enforced in the save transaction, the lang keys and the docs.
Purpose: SC-1, SC-2, SC-3 (seam); D-01..D-20.
Output: four framework commits (each with its README and docs changes and a smoke test), no change to commands.go, token_commands.go or account.go.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-CONTEXT.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-RESEARCH.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-PATTERNS.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-UI-SPEC.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md
Concurrency and commits: 19.6, 19.7 and 19.8 are in flight and touch cabana. Before each task run `git status --porcelain -- modules/cabana modules/pact modules/phrasebook docs` and `git log --oneline -5 -- modules/cabana/http.go modules/cabana/crud.go`; stop and report when a file this task edits carries someone else's uncommitted change. Re-read the `crud.go` save order (lines ~771-848) and `Activate` (http.go:74-210) before editing them (RESEARCH "Valid until"). Record the plan base as `PLAN_BASE=$(git rev-parse HEAD)` before Task 1. Stage by explicit path, never `git add -A`; leave STATE.md and state.json alone; no co-author tags; READMEs and docs in the same commit as the code they describe; `go vet ./...`, `go test -short ./...` and `go -C examples/hello test ./...` green at every commit. Docker-backed tests skip under -short; on this host run them, through `sudo -u golem` if the Docker socket is denied (CLAUDE.md); a Docker test that could not run is NOT RUN in the SUMMARY, never passed.
Reference only (read, never modify): `../sm-grzybyfunkcjonalne-app/plugins/golem15/user/` (abbreviated USER/ below) and `../examples/golem15-wintercms-starter/modules/backend/`.
<interfaces>
- pact (modules/pact/capabilities.go): `AdminController{ID, ModelName, ConfigDir}` :179, `HasAdminControllers` :186, `AdminAssets{AdminFS() fs.FS}` :192, `AdminPermissioned` :197, `AdminRecordSource{NewRecord() any}` :203, `AdminBulkAction{Name, Label, Confirm, Permissions, Run, Detached}` :286 with `AdminBulkActionInput{Records []any}` and `AdminBulkActionResult{Message, Affected}`, `HasAdminBulkActions` :325, `AdminRecordAction{Name, Label, Confirm, Permissions, Danger, Applies, Run}` :343, `AdminRecordActionInput{RecordID uint64, Record any}` :360, `AdminRecordActionResult{Message}` :368, `HasAdminRecordActions` :374, `AdminPartialData.PartialData(ctx, name, record)` :385, `Permission{Code, Tab, Label, Roles}` :512, `HasPermissions` :520, `SettingsItem{Code, Label, Description, Category, Icon, Model, Order, Keywords, Permissions, Controller, Form, NewModel}` :570, `HasSettings` :590, `ListExtendQuery` :720, `RowState` constants `RowStateDeleted`, `RowStateDisabled` :726-741, `ListRowStates` :744, `FormRecordTitle` :772, `FormExtendQuery` :777, `FormBeforeCreate/Update`, `FormAfterCreate/Update`, `FormBeforeDelete`, `FormAfterDelete` :782-808, `FormVirtualFields` :831, `FormRules(ctx, op)` :842, `FilterScope{FilterScopes() []string; FilterScope(name, db, value) *gorm.DB}` and `FilterOptions(scope) []Option` (near :908-922).
- party (modules/party/registry.go:16-21): `Plugin{ID() string; Requires() []string; Register(*backpack.App) error; Boot(*backpack.App) error}`.
- cabana: `Activate(app, plugins) (*Routes, error)` http.go:74 (early return `if len(items) == 0` :79, compileRegistryWithTypes :103, checkDeferredModels :122, compileContributions :125, compileTokenScopes :128, validateMessageKeys :135); `formSchema` http.go:753 (per-request `kept` slice :771-785), `listSchema` http.go:801 (new slices for toolbar and bulk actions), `protect` http.go:1101; `collectControllers` registry.go:22 (controller ID must start with plugin ID), `reservedVendorSegments` registry.go:46, `compilePermissionCatalog` registry.go:~212-246, `compileContributions` registry.go:268 (validates RecordActions permissions :342-346, settings link controllers), `permissionCode` :366, `rolePermissions(role) map[string]bool` :449; `compileExtension` extension.go:60 (preview rule :91-93), `compileRecordActions` extension.go:405, `builtinToolbarActions` extension.go:47, `virtualFieldTypes` and `checkVirtualFields` extension.go:156-175; `CompiledController` contracts.go:97 (Form.recordActions is unexported `[]string`, schema_types.go:159; RecordActions map), `Registry` contracts.go:146, `Allows`, `granted` contracts.go:214-248, `requiredOf`; `BackendUser`, `BackendUserRole` contracts.go:17-58 (BackendUser has a `Role BackendUserRole` association field); `FindByID` merge auth.go:39-67, `applyUserPermissions` auth.go:73, `principalFrom` auth.go:101, `parseGrants` auth.go:118, `adminBcryptCost(app)` auth.go:481; `ValidationError{Details}` crud.go:69, `ForbiddenError{Message, Details}` crud.go:83 (Message is a phrase key or text, no params), `localizeControllerError` crud.go:98; `offeredRecordActions` crud.go:551, `RecordAction` crud.go:586, `projectFullRecord` crud.go:647, `save` crud.go:672 (order: Fill, Validate, formBeforeUpdate :800, applyPermissionValues :809, checkRelationScope, checkRelationLocks :819, assignBelongsTo :822, `tx.Unscoped().Save` :829, formAfterUpdate :848), `newWritableModel` crud.go:907 (model needs lagoon.HasFillable and Rules), `loadRecord` crud.go:937 (FormExtendQuery then FOR UPDATE), `deleteRecord` crud.go:1127 (GORM Delete on the tx root, soft delete), bulk delete loop crud.go:355-373, `protectedFillKey` crud.go:1407 (role_id, permissions, is_superuser, is_system, is_activated, password); `recordActionOf` actions.go:242; `TxFromContext`, `VirtualFieldsFromContext` (only keys present in the body) tx_context.go:27,54; `PermissionOption{Code, Label, Tab, Comment, Locked}`, `PermissionEditorProvider{AdminPermissionOptions, AdminPermissionValues, AdminSetPermissionValues}`, `applyPermissionValues` field_permission.go:25-65, 260; `FieldRelationContract{Field, Kind, NewRelated, ForeignKey, LabelColumn, WritableForeignKey}`, `RelationLock{IDs, Message}`, `RelationLockProvider.AdminRelationLocks(ctx, field)` relation_field.go:22-66; `assetPath` schema.go:41 (`~/plugins/summercms/cabana/x` resolves to `x` in the AdminFS); `compileSettingLink` settings.go:57; `passwordMaxBytes = 72` and `accountMessage(ctx, tr, rule, field, params)` account.go:47, 335-336; admin:create uniqueness query commands.go:96 and defaults :106-115, reset cutoff `time.Now().UTC().Add(time.Second)` commands.go:157.
- phrasebook: `(*Translator).Choice(ctx, key, count, params map[string]string) string`, `Get(ctx, key, params)` translator.go:152-178.
- bouncer: `Principal{ID, Backend, IsSuperuser, PermissionGrants map[string]bool, Scope *ScopeLimit{Full bool; Scopes}}` context.go:26-35, `User(ctx)` :46, `HashPassword(cost, plain)`.
- Test helpers (package cabana_test, modules/cabana/auth_test.go): `adminGorm(t)` :321 (skips under -short), `adminHandler(t, gdb, mutate)` :367, `insertAdmin(t, gdb, login, email, password, activated, deleted)` :403 (developer role, never superuser), `demoPlugin`; env pattern `newActEnv` with `phrasebook.Activate`, `surf.Assemble`, `postJSON`, `adminAPI`, `accessToken`, `adminTestPassword` in phase101_actions_test.go:233-346. Internal fixtures (package cabana): `contributionPlugin`, `metadataRegistry`, `metadataPlugin` used in settings_link_test.go:1-60. External hooks into internals go through modules/cabana/export_test.go.
- Analogs: USER/admin.go:11-38 (embed + AdminFS + interface asserts), USER/controllers/users_admin_controller.go (identity :41-51, unscoped queries :85-97, row states :102-129, virtual fields and rules :135-155, FilterOptions :168, guardPrivilegedMember :193-209, FormBeforeUpdate/Create :217-275, storePassword :303-321, AdminFieldRelations :370, AdminRelationLocks :402-422, permission provider :426-477, stored-row re-read :479-497, FormBeforeDelete :524, bulk actions :589-627, record actions :665-743, PartialData :746), USER/controllers/users/{config_list,config_form,config_filter}.yaml, USER/controllers/users/_hint.htm, USER/models/user/{columns,fields}.yaml, USER/controllers/usergroups/*.yaml; Winter `modules/backend/controllers/Users.php`, `UserRoles.php`, `models/user/fields.yaml`, `models/userrole/fields.yaml`, `formwidgets/PermissionEditor.php` (~93-150).
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: a superuser opens Settings → System → Administrators, browses administrators, opens one's preview, and runs a record action another plugin contributed through the new pact seam</name>
<reversibility rating="costly">D-14 publishes a pact capability that 19.7 and the MFA plugin implement, and D-01's permission codes get stored in role and user permission JSON on every install that grants them.</reversibility>
<files>modules/pact/capabilities.go, modules/pact/README.md, modules/cabana/action_extensions.go, modules/cabana/backend_screens.go, modules/cabana/backend_users_admin.go, modules/cabana/backendscreens/controllers/users/config_list.yaml, modules/cabana/backendscreens/controllers/users/config_form.yaml, modules/cabana/backendscreens/models/user/columns.yaml, modules/cabana/backendscreens/models/user/fields.yaml, modules/cabana/http.go, modules/cabana/registry.go, modules/cabana/contracts.go, modules/cabana/backend_screens_test.go, modules/cabana/backend_screens_smoke_test.go, modules/phrasebook/backend/lang/en/lang.yaml, modules/phrasebook/backend/lang/pl/lang.yaml, modules/cabana/README.md, docs/backend/admin-controllers.md</files>
<read_first>modules/pact/capabilities.go (:179-205, :325-390, :505-595), modules/pact/README.md (Features line 30, API table :130-140), modules/cabana/http.go (:74-210), modules/cabana/registry.go (:1-80, :205-360, :440-460), modules/cabana/extension.go (:30-130, :395-429), modules/cabana/contracts.go, modules/cabana/crud.go (:546-640), modules/cabana/actions.go (:230-270), modules/cabana/settings.go (:57-65), modules/cabana/navigation.go (:43-152), modules/cabana/schema.go (:30-60), modules/cabana/settings_link_test.go, modules/cabana/export_test.go, modules/cabana/phase101_actions_test.go (:225-346), modules/cabana/auth_test.go (:314-440), modules/surf/admin_prefix_test.go (:91-99, :194), modules/phrasebook/backend/lang/en/lang.yaml and pl/lang.yaml (top-level groups, settings: and permissioneditor: blocks), USER/admin.go, USER/controllers/users/config_list.yaml, USER/controllers/users/config_form.yaml, USER/models/user/columns.yaml, USER/models/user/fields.yaml, docs/backend/admin-controllers.md (:464-500), modules/cabana/README.md, 19.6.1-UI-SPEC.md (Copywriting Contract, C1-C4), 19.6.1-RESEARCH.md (Patterns 1 and 2, Pitfalls 3 and 8, Code Examples "A contribution from another plugin")</read_first>
<action>Per D-01, D-02, D-04, D-14, D-15 and SC-3.
pact (capabilities.go, additive, next to HasAdminRecordActions): `type AdminActionExtension struct { Controller string; RecordActions []AdminRecordAction }` and the plugin-level `type HasAdminActionExtensions interface { AdminActionExtensions() []AdminActionExtension }`. Doc comments: Controller is the ID (vendor.plugin.controller) of any activated plugin's admin controller, cabana's own included; each action keeps its own Permissions, Applies, Confirm and Danger and runs exactly like the owner's record actions; contributions are offered on the target's preview screen after the owner's actions, in plugin activation order; the admin also checks the target controller's RequiredPermissions; record actions only for now, the struct leaves room for more kinds. The comment names no consumer. pact README: extend the admin-extension Features sentence and add two API-table rows.
cabana/action_extensions.go (new): `compileActionExtensions(reg *Registry, plugins []party.Plugin) error`. For each plugin implementing HasAdminActionExtensions, in slice order, for each extension: the target is `reg.byID[ext.Controller]`, a missing one is an error naming the plugin and the controller ID; the target needs a compiled form with a preview block (error otherwise: record actions are offered on the preview screen); each action passes the checks compileRecordActions applies (identifier name, not create or delete, non-nil Run) and has a non-empty Label; a name already present in the target's RecordActions (the owner's or an earlier contribution) is an error naming both plugins. Merge by building a new recordActions slice for the target form (copy, then append the name; never append into the compiled backing array) and adding the action to `cc.RecordActions`; keep the contributing plugin per name in a new unexported map on CompiledController (contracts.go) for the error text. offeredRecordActions, recordActionOf and RecordAction then serve contributed actions unchanged; do not edit them.
registry.go: compilePermissionCatalog also appends each new code to a new unexported `permissionOrder []string` on Registry (contracts.go), in activation order, for stable editor display.
cabana/backend_screens.go (new): exported constants `PermissionManageUsers = "backend.manage_users"`, `PermissionManageRoles = "backend.manage_roles"`, `AdministratorsControllerID = "summercms.cabana.users"`, `RolesControllerID = "summercms.cabana.roles"`; the unexported pseudo-plugin `backendScreens{app *backpack.App; reg *Registry}` implementing party.Plugin (ID "summercms.cabana", Requires nil, no-op Register and Boot), pact.AdminAssets (AdminFS returns fs.Sub of a go:embed of the backendscreens directory, so paths start at controllers/ and models/), pact.HasAdminControllers (the Administrators controller; Roles joins in Task 4), pact.HasPermissions and pact.HasSettings, with compile-time interface assertions. Permissions(): both codes, Tab backend::lang.permissions.tab_system, Label backend::lang.permissions.manage_users and backend::lang.permissions.manage_roles; leave the Roles field of both entries empty (D-04). Settings(): one link entry, Code administrators, Controller AdministratorsControllerID, Permissions [PermissionManageUsers], Label backend::lang.user.menu_label, Description backend::lang.user.menu_description, Category backend::lang.settings.category_system, Icon users, Order 100 (D-02; the roles entry joins in Task 4). The pseudo-plugin declares no navigation entry (D-02). `withBackendScreens(app, plugins) ([]party.Plugin, *backendScreens)` returns a new slice with the pseudo-plugin appended (the caller's slice is never mutated).
http.go Activate: keep the `len(items) == 0` early return exactly where it is (no controllers: no admin routes, no secret check; examples/hello and surf's admin-prefix test rely on it). After it call withBackendScreens, re-run collectControllers on the extended slice and use that slice for every later step (checkReservedSegments, compileFieldTypes, compileRegistryWithTypes, checkDeferredModels, the new compileActionExtensions placed after checkDeferredModels and before compileContributions, compileContributions, compileTokenScopes). After compileContributions set `screens.reg = reg`. Update Activate's doc comment.
cabana/backend_users_admin.go (new): `usersAdmin{screens *backendScreens}` (lazy app and registry access, as the analog's app func): ID AdministratorsControllerID, ModelName `Backend\Models\User`, ConfigDir controllers/users, RequiredPermissions [PermissionManageUsers], NewRecord `&BackendUser{}`, FormRecordTitle ("First Last" when either name is set, else the login), AdminFieldRelations with the role contract (Field role, Kind belongsTo, NewRelated BackendUserRole, ForeignKey role_id, LabelColumn name, WritableForeignKey true). contracts.go: BackendUser gains `Fillable()` (login, email, first_name, last_name) and `Rules()` (login required|max:255, email required|email|max:255); BackendUserRole gains `Fillable()` (name, code, description) and `Rules()` (name required|max:255|unique:backend_user_roles, code required|max:255). Methods only; no struct field changes in this task.
YAML under backendscreens/ (shapes from USER/controllers/users/*.yaml and UI-SPEC C2-C4): controllers/users/config_list.yaml (list models/user/columns.yaml, modelClass `Backend\Models\User`, title backend::lang.user.list_title, recordUrl summercms/cabana/users/preview/:id, noRecordsMessage backend::lang.user.list_empty, recordsPerPage 20, showSetup true, toolbar search prompt backend::lang.list.search_prompt and no buttons yet: create and delete come with their guards in Tasks 2 and 3); controllers/users/config_form.yaml (name backend::lang.user.name, form models/user/fields.yaml, modelClass, defaultRedirect summercms/cabana/users, update redirect summercms/cabana/users/update/:id and redirectClose summercms/cabana/users/preview/:id, a preview block, messages update/saved/edit backend::lang.user.edit/preview backend::lang.user.preview); models/user/columns.yaml in UI-SPEC C2 order (login, first_name, last_name, email searchable and sortable; role relation role select name sortable; last_login datetime sortable; is_superuser switch; created_at datetime invisible sortable); models/user/fields.yaml Account tab (backend::lang.user.account) with login left, email right, first_name left, last_name right, role (type relation, nameFrom name, emptyOption backend::lang.user.role_none, comment backend::lang.user.role_comment, span full) and last_login (datepicker, context preview, span left).
Lang (en and pl, modules/phrasebook/backend/lang): every key of UI-SPEC "Copywriting Contract" except the "Framework additions (D-21)" table, which Plan 02 adds: new groups `user` (with the nested `role` block) and `permissions`, and `settings.category_system`. Plural texts are CLDR maps (en one/other, pl one/few/many/other), placeholders :name and :count, values verbatim from the UI-SPEC tables.
Smoke tests (Plan 03 extends both files). modules/cabana/backend_screens_test.go (package cabana, Docker-free): `TestBackendScreensSmoke` compiles, through withBackendScreens and the same compile steps Activate uses (or an unexported helper factored out of Activate for both), an extender plugin that owns one controller with a form and preview (reuse an existing internal fixture where one fits) and contributes record action acme_ping to AdministratorsControllerID with a permission it declares; asserts the Administrators controller's compiled recordActions end with acme_ping and RecordActions holds it; the catalog holds both backend codes and roleGrants for developer and publisher hold neither; settingsList for a superuser principal lists administrators with controller summercms.cabana.users and for a backend principal without grants does not; a contribution to acme.missing.things fails compilation with an error containing that ID; Activate with a plugin that registers no admin controllers returns nil routes and a nil error. modules/cabana/backend_screens_smoke_test.go (package cabana_test, Postgres through adminGorm): `TestBackendScreensPostgresSmoke` assembles like newActEnv (phrasebook.Activate, surf.Assemble) with demoPlugin plus a contributor plugin whose acme_ping Run asserts cabana.TxFromContext and records RecordID; seeds a superuser (insertAdmin, then UPDATE is_superuser = TRUE) and a developer-role admin with unique logins; as the superuser: GET /summercms/cabana/users lists both, GET /summercms/cabana/users/{id} carries acme_ping in meta.actions, POST /summercms/cabana/users/{id}/actions/acme_ping answers 200 and Run saw that id, GET /settings lists administrators; as the developer admin GET /summercms/cabana/users answers 403.
Existing tests: every test that assembles the admin with a controller-owning plugin now also activates the pseudo-plugin. If an existing test enumerates a superuser's settings entries or the permission catalog, update only that expectation to include the new entries and name each file in the SUMMARY; any other change in an existing test's result is a regression to fix in the code.
Docs in this commit: cabana README (Features: the built-in Administrators screen and cross-controller record actions; API reference rows for the four constants and the new BackendUser and BackendUserRole methods; the screens activate only when a plugin has admin controllers); docs/backend/admin-controllers.md, after "Record actions", a section "Adding record actions to another controller" with a neutral acme example contributing to summercms.cabana.users, the boot errors, that the target's RequiredPermissions and the action's own Permissions both apply, that a contributed name must not reuse one the owner registers, that a record action carries no input beyond the record, and that contributions are compiled only when the admin is active. Commit: `feat(cabana): Administrators screen and cross-controller record actions`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./modules/cabana/... ./modules/pact/... ./modules/surf/... &amp;&amp; go test -count=1 -short -run '^TestBackendScreensSmoke$' -v ./modules/cabana 2&gt;&amp;1 | grep -q -- '^--- PASS: TestBackendScreensSmoke' &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>Non-zero exit from vet, the short suites, hello or the docs checker, or no "--- PASS: TestBackendScreensSmoke" line (the smoke failed or matched no test).</fails_when>
<automated>go test -count=1 -run '^TestBackendScreensPostgresSmoke$' -v ./modules/cabana 2&gt;&amp;1 | grep -q -- '^--- PASS: TestBackendScreensPostgresSmoke' &amp;&amp; go test -count=1 ./modules/cabana ./modules/surf</automated>
<fails_when>No "--- PASS: TestBackendScreensPostgresSmoke" line (failed, or "--- SKIP" because Docker was unreachable: rerun through sudo -u golem, else record NOT RUN), or the full Docker-backed cabana and surf suites exit non-zero.</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'type HasAdminActionExtensions interface' modules/pact/capabilities.go` prints 1 and `grep -c 'type AdminActionExtension struct' modules/pact/capabilities.go` prints 1.
- `grep -c 'func compileActionExtensions(' modules/cabana/action_extensions.go` prints 1.
- `awk '/compileActionExtensions\(reg/{a=NR} /compileContributions\(reg/{c=NR} END{exit !(a &amp;&amp; c &amp;&amp; a&lt;c)}' modules/cabana/http.go` exits 0 (the merge runs before permission validation).
- `awk '/len\(items\) == 0/{e=NR} /withBackendScreens\(/{w=NR} END{exit !(e &amp;&amp; w &amp;&amp; e&lt;w)}' modules/cabana/http.go` exits 0 (the pseudo-plugin is appended after the no-controllers return).
- `grep -v '^\s*//' modules/cabana/backend_screens.go | grep -c 'Roles:'` prints 0.
- `grep -c '"backend.manage_users"' modules/cabana/backend_screens.go` prints 1 and `grep -c '"summercms.cabana.users"' modules/cabana/backend_screens.go` prints 1.
- `grep -c 'pact.HasNavigation' modules/cabana/backend_screens.go` prints 0.
- `grep -c '^user:' modules/phrasebook/backend/lang/en/lang.yaml` prints 1, `grep -c '^user:' modules/phrasebook/backend/lang/pl/lang.yaml` prints 1, `grep -c '^permissions:' modules/phrasebook/backend/lang/en/lang.yaml` prints 1, and `grep -c 'category_system' modules/phrasebook/backend/lang/pl/lang.yaml` prints 1.
- `grep -c 'summercms.cabana.users' docs/backend/admin-controllers.md` prints at least 1 and `grep -c 'HasAdminActionExtensions' modules/pact/README.md` prints at least 1.
- `git log --format=%h "$PLAN_BASE"..HEAD -- modules/cabana/commands.go modules/cabana/token_commands.go modules/cabana/account.go` prints nothing.
</acceptance_criteria>
<done>A superuser finds Administrators under Settings → System, lists administrators, opens a preview and runs a plugin-contributed record action; admins without the code get 403; apps without admin controllers boot as before.</done>
</task>
<task type="auto">
<name>Task 2: Administrators form: create and edit administrators with passwords, role and per-user permission overrides, under every save-path privilege rule</name>
<reversibility rating="reversible">Hooks and unexported helpers inside cabana; no published surface beyond the screen's behaviour.</reversibility>
<files>modules/cabana/backend_privileges.go, modules/cabana/backend_users_admin.go, modules/cabana/contracts.go, modules/cabana/crud.go, modules/cabana/http.go, modules/cabana/backendscreens/models/user/fields.yaml, modules/cabana/backendscreens/controllers/users/config_list.yaml, modules/cabana/backendscreens/controllers/users/config_form.yaml, modules/cabana/backendscreens/controllers/users/config_filter.yaml, modules/cabana/backend_screens_smoke_test.go, modules/cabana/README.md, docs/backend/users-and-permissions.md</files>
<read_first>modules/cabana/backend_users_admin.go and backend_screens.go (from Task 1), modules/cabana/crud.go (:647-870, :907-960, :1095-1145, :1180-1210, :1354-1420), modules/cabana/http.go (:753-845), modules/cabana/field_permission.go, modules/cabana/relation_field.go (:20-110), modules/cabana/tx_context.go, modules/cabana/auth.go (:33-140, :476-492), modules/cabana/commands.go (:85-170), modules/cabana/account.go (:40-50, :320-345, :470-480), modules/cabana/contracts.go (:200-250), modules/cabana/filter_schema.go (:290-330), modules/cabana/query.go (:480-495), modules/bouncer/context.go, modules/lagoon/validate.go (:355-380), USER/controllers/users_admin_controller.go (:85-155, :168-320, :370-522), USER/controllers/users/config_filter.yaml, USER/models/user/fields.yaml, ../examples/golem15-wintercms-starter/modules/backend/controllers/Users.php, ../examples/golem15-wintercms-starter/modules/backend/formwidgets/PermissionEditor.php (:93-150), docs/backend/users-and-permissions.md (:39-76, :134-160), 19.6.1-RESEARCH.md (Pattern 3 hook table, Pattern 4, Pattern 5, Pitfalls 1, 2, 4, 9), 19.6.1-UI-SPEC.md (C3, C8)</read_first>
<action>Per D-03, D-05, D-06, D-07, D-08, D-09 (demotion path), D-11, D-12 (password set), D-19 and D-20.
backend_privileges.go (new):
- `actsAsSuperuser(p *bouncer.Principal) bool` is `p != nil &amp;&amp; p.IsSuperuser &amp;&amp; (p.Scope == nil || p.Scope.Full)`; every superuser test on these screens uses it (Allows lets a superuser through, and a superuser's scoped personal token must not get superuser-only powers).
- `roleGrantSet(reg *Registry, role BackendUserRole) map[string]bool`: parseGrants(role.Permissions) plus reg.rolePermissions(role.Code), the FindByID merge.
- `effectiveGrants(ctx, db *gorm.DB, reg *Registry, user *BackendUser) (map[string]bool, error)`: loads the role by user.RoleID into its own BackendUserRole value through `db.Session(&amp;gorm.Session{NewDB: true})`, never onto the record being saved (GORM's Save would rewrite role_id from a loaded association, RESEARCH anti-pattern), then applyUserPermissions(grants, user.Permissions).
- `grantsCovered(holder, wanted map[string]bool) bool`: each non-wildcard code in wanted must pass granted(holder, code); a wildcard grant in wanted (ending in ".*" or starting with "*") is covered only when holder holds the same wildcard or a broader prefix wildcard (RESEARCH A3); a wildcard from wanted is never passed to granted or Allows.
- `guardOutranked(ctx, reg, editor *bouncer.Principal, target *BackendUser) error` (D-19): nil for actsAsSuperuser editors and for the editor's own record; otherwise the target's effectiveGrants must be grantsCovered by editor.PermissionGrants, else `&amp;ForbiddenError{Message: "backend::lang.user.outranked"}`.
- `guardLastSuperuser(ctx, target *BackendUser) error` (D-09): nil unless the target is currently a superuser, activated and not deleted; then, through the write transaction from TxFromContext (none is an error), `SELECT pg_advisory_xact_lock(?)` with the fixed int64 `superuserLockKey` (documented beside it), and count other rows with `is_superuser AND is_activated AND id &lt;&gt; ?` (the default scope adds deleted_at IS NULL); zero answers `&amp;ForbiddenError{Message: "backend::lang.user.last_superuser"}`. The caller always holds its row lock first and nothing takes a row lock after the advisory lock, so there is no wait cycle (RESEARCH Pattern 4).
- `revocationCutoff() time.Time` returns `time.Now().UTC().Add(time.Second)` (admin:reset-password's value).
contracts.go: BackendUser gains unexported fields `priorRoleID *uint` and `priorLoaded bool` (GORM and cabana's model reflection skip unexported fields) and `FilterScopes()` returning filterByRole with `FilterScope(name, db, value)` narrowing to `backend_users.role_id = value` (anything else matches nothing, a nil db passes through), as USER models/user.go:112-129 does for groups.
crud.go and http.go, unexported in-package hooks (no pact surface; other controllers' payloads unchanged):
- `recordDataExtender` with `extendRecordData(ctx context.Context, record any, data map[string]any)`, called by projectFullRecord after the scalar projection when the controller implements it.
- `schemaFieldHider` with `hiddenSchemaFields(ctx context.Context) map[string]bool`, applied in formSchema while building `kept` and in listSchema to columns and filters, always into new slices (the cached schema is never mutated).
backend_users_admin.go:
- `extendRecordData`: for actsAsSuperuser principals set data["is_superuser"] to the stored flag (Pitfall 1: the virtual switch must load its value or an untouched save demotes). `hiddenSchemaFields`: is_superuser for every non-superuser (D-03).
- ListExtendQuery and FormExtendQuery: for non-superusers add `Where("backend_users.is_superuser = ?", false)` (qualified, query.go:485-490), so a superuser id is not found for them on show, partials, update, delete and record actions, and never listed (D-03). Task 3 makes both unscoped for the trashed view.
- FormVirtualFields: password, password_confirmation, is_superuser. FormRules(ctx, op): password `required|min:4|confirmed` on create and `nullable|min:4|confirmed` on update (D-11); login and email uniqueness is checked by hand below, never with lagoon's unique rule (Pitfall 4). FilterOptions(scope): for filterByRole every role by name (USER :168-191 pattern).
- AdminRelationLocks(ctx, "role") (D-07): zero lock for superusers; otherwise lock the ids of every role whose roleGrantSet the editor's grants do not cover, Message backend::lang.user.role_ceiling. (Locks, not option filtering, so a target's current above-ceiling role still saves unchanged.)
- PermissionEditorProvider (mode radio, D-06, D-07): AdminPermissionOptions lists the catalog in permissionOrder (Label and Tab from pact.Permission; Comment backend::lang.permissions.manage_users_comment and manage_roles_comment for the two backend codes) with `Locked: !actsAsSuperuser(editor) &amp;&amp; !granted(editor.PermissionGrants, code)`; AdminPermissionValues decodes BackendUser.Permissions JSON into code to 1 or -1 (other values dropped); AdminSetPermissionValues refuses on the editor's own record any next set that differs from the stored one with `&amp;ForbiddenError{Message: "backend::lang.user.self_privileged", Details: {"permissions": [same key]}}` (D-08) and otherwise stores compact JSON, `{}` when empty.
- FormBeforeCreate: virtual is_superuser present and not actsAsSuperuser answers 403 superuser_forbidden with Details is_superuser, whatever its value (D-05); a superuser sets IsSuperuser from it; IsActivated true and ActivatedAt now (admin:create, commands.go:106-115); password: longer than passwordMaxBytes answers 422 on password with accountMessage(ctx, tr, "max.string", "password", max 72), else bouncer.HashPassword(adminBcryptCost(app), plain); then the identity check. No mail is sent (D-13).
- FormBeforeUpdate (the model still holds the stored role_id, is_superuser, is_activated and permissions: protected keys are never filled): record priorRoleID and priorLoaded; guardOutranked for a target that is not the editor (D-19); is_superuser present: not actsAsSuperuser answers 403 superuser_forbidden (D-05); own record with a different value answers 403 self_privileged with Details is_superuser (D-08); stored true and submitted false calls guardLastSuperuser before the flag changes, its refusal carrying Details is_superuser (D-09); then set IsSuperuser. A non-empty password: own record answers 403 `backend::lang.user.own_password` with Details password (D-20); over 72 bytes answers 422; else hash it and set TokensValidAfter to revocationCutoff() (D-11, D-12). Then the identity check.
- Identity check (create and update): one unscoped, case-insensitive count like admin:create (commands.go:96) excluding the record's own id; a held login answers 422 on login with backend::lang.user.login_taken_deleted when only a deleted admin holds it, else login_taken; the same for email with email_taken_deleted and email_taken.
- FormAfterUpdate: on the editor's own record a RoleID different from priorRoleID answers 403 self_privileged with Details role, which rolls the save back (D-08; assignBelongsTo runs after FormBeforeUpdate, Pitfall 2).
YAML: fields.yaml adds password (left) and password_confirmation (right) as type password, context [create, update], comment backend::lang.user.password_comment on password; and the Permissions tab (backend::lang.user.permissions) with is_superuser (switch, label backend::lang.user.superuser, comment backend::lang.user.superuser_comment, span full) and permissions (permissioneditor, mode radio, label backend::lang.user.permission_overrides, comment backend::lang.user.permission_overrides_comment, span full); no trigger key yet (Plan 02). config_list.yaml: toolbar buttons [create], messages create backend::lang.user.new. config_form.yaml: create redirect summercms/cabana/users/preview/:id, redirectClose summercms/cabana/users, messages create backend::lang.user.new. config_filter.yaml (supported types only): role (scope filterByRole, modelClass `Backend\Models\UserRole`, nameFrom name, label backend::lang.user.role_field), is_superuser (switch, column is_superuser, label backend::lang.user.superuser), activated (switch, column is_activated, label backend::lang.user.activated), last_login (daterange, column last_login, label backend::lang.user.last_login); config_list.yaml gets `filter: config_filter.yaml`.
Smoke (append to backend_screens_smoke_test.go): `TestAdministratorsFormSmoke`: the superuser creates an admin with password and confirmation (201, the stored hash verifies with bouncer, is_activated true); GET of the superuser's own record returns data.is_superuser true; a developer-role admin whose role JSON grants backend.manage_users gets 404 on GET of the superuser's id, no is_superuser in the form schema fields, and 403 when updating a non-superuser with is_superuser false in the body; the superuser switching off its own flag answers 403 with the self_privileged text; with a second superuser seeded, demoting it through update answers 200 and its record then reads is_superuser false. (A sequential request cannot reach the last-superuser refusal through the screens, see Decisions; Plan 03 proves each path with a held-lock race.)
Docs in this commit: docs/backend/users-and-permissions.md "Permissions" lists backend.manage_users and backend.manage_roles (granted to no role by default, so only superusers reach the screens until someone grants them); "Managing administrators" gains the Administrators screen (Settings → System, the form, passwords, and the rules D-05, D-07, D-08, D-19, D-20; a password set ends the target's sessions but not their personal API tokens, as admin:reset-password); the My account paragraph (~line 138) now says role, superuser flag, permissions and activation are changed by another administrator on the Administrators screen or with the operator commands. cabana README Features. Commit: `feat(cabana): administrator form with server-side privilege rules`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./modules/cabana/... ./modules/surf/... &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>Non-zero exit from vet, the short suites, hello or the docs checker.</fails_when>
<automated>go test -count=1 -run '^(TestBackendScreensPostgresSmoke|TestAdministratorsFormSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>Fewer than 2 top-level "--- PASS: Test" lines: a smoke failed, or both printed "--- SKIP" because Docker was unreachable (rerun through sudo -u golem, else record NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'pg_advisory_xact_lock' modules/cabana/backend_privileges.go` prints at least 1 and `grep -c 'func guardLastSuperuser(' modules/cabana/backend_privileges.go` prints 1.
- `grep -c 'func actsAsSuperuser(' modules/cabana/backend_privileges.go` prints 1 and `grep -c 'Scope.Full' modules/cabana/backend_privileges.go` prints at least 1.
- `grep -v '^\s*//' modules/cabana/backend_users_admin.go | grep -c 'Preload("Role")'` prints 0 and the same count on backend_privileges.go prints 0.
- `grep -c 'backend_users.is_superuser = ?' modules/cabana/backend_users_admin.go` prints at least 1.
- `grep -c 'extendRecordData(' modules/cabana/crud.go` prints at least 1 and `grep -c 'hiddenSchemaFields(' modules/cabana/http.go` prints at least 2.
- `grep -cE 'func \([a-z]+ \*?usersAdmin\) FormAfterUpdate\(' modules/cabana/backend_users_admin.go` prints 1.
- `grep -c 'mode: radio' modules/cabana/backendscreens/models/user/fields.yaml` prints 1 and `grep -c 'type: password' modules/cabana/backendscreens/models/user/fields.yaml` prints 2.
- `grep -c 'backend.manage_roles' docs/backend/users-and-permissions.md` prints at least 1 and `grep -ci 'personal API tokens' docs/backend/users-and-permissions.md` prints at least 2.
</acceptance_criteria>
<done>A superuser creates administrators with passwords and edits role, overrides and superuser access; a limited admin is held to D-03, D-05, D-07, D-08, D-19 and D-20 by the server; the last superuser cannot be demoted through the form.</done>
</task>
<task type="auto">
<name>Task 3: Administrators lifecycle: activate, deactivate, delete and restore administrators, with the last active superuser protected on every path and access cut at once</name>
<reversibility rating="reversible">Controller actions and hooks inside cabana; soft delete keeps every row restorable.</reversibility>
<files>modules/cabana/backend_users_admin.go, modules/cabana/contracts.go, modules/cabana/backendscreens/controllers/users/config_list.yaml, modules/cabana/backendscreens/controllers/users/config_form.yaml, modules/cabana/backendscreens/controllers/users/config_filter.yaml, modules/cabana/backendscreens/controllers/users/_hint.htm, modules/cabana/backend_screens_smoke_test.go, modules/cabana/README.md, docs/backend/users-and-permissions.md</files>
<read_first>modules/cabana/backend_users_admin.go and backend_privileges.go (from Task 2), modules/cabana/crud.go (:340-380, :586-640, :937-960, :1127-1145), modules/cabana/actions.go, modules/cabana/partial_render.go, modules/cabana/token_guard.go (:90-110), modules/bouncer/jwt.go (:140-175), modules/pact/capabilities.go (:276-330, :720-760), USER/controllers/users_admin_controller.go (:85-129, :524-556, :589-770), USER/controllers/users/_hint.htm, USER/controllers/users/config_list.yaml, USER/controllers/users/config_form.yaml, docs/backend/admin-controllers.md (Bulk actions, Record actions), 19.6.1-UI-SPEC.md (C2, C4, C8, Administrators preview copy), 19.6.1-RESEARCH.md (Pattern 3 hook table rows for delete and actions, Pitfall 7)</read_first>
<action>Per D-08 (self delete and deactivate), D-09, D-10, D-12, D-15 and D-19.
backend_users_admin.go:
- ListExtendQuery and FormExtendQuery start from `db.Unscoped()` and keep the D-03 filter, so deleted admins are listed, opened and restorable (the row write keeps the load scope, crud.go:827-829).
- ListRowStates: pact.RowStateDeleted when DeletedAt.Valid, pact.RowStateDisabled when not activated (no other state).
- FormBeforeDelete (called once per row inside bulk delete, so one refusal rolls back the whole selection): the editor's own record answers 403 with Message backend::lang.user.self_delete (D-08); guardOutranked (D-19); guardLastSuperuser (D-09). FormAfterDelete: set tokens_valid_after to revocationCutoff() on the now soft-deleted row through `tx.Session(&amp;gorm.Session{NewDB: true}).WithContext(ctx).Unscoped()` (D-12). The generic delete stays GORM's soft delete on the tx root; never hard-delete a backend user (D-10; tokens, linked_by and attachments keep their foreign keys). Do not copy the analog's hard-deleting FormAfterDelete.
- AdminRecordActions and AdminBulkActions: activate, deactivate and restore, with UI-SPEC labels, confirms and success messages (user.activate / activate_confirm / activate_selected_confirm / activated / activated_selected, and the same pattern for deactivate and restore); none is Danger. Applies: activate when not deleted and not activated; deactivate when not deleted, activated and not the editor's own record; restore when deleted. Run, for the record and for each bulk row, through cabana.TxFromContext (the analog's builder closures, USER :589-743): deactivate answers 403 self_deactivate on the editor's own record (bulk path), then guardOutranked and guardLastSuperuser, then sets is_activated false and tokens_valid_after revocationCutoff() (D-12); activate runs guardOutranked, then sets is_activated true and activated_at now, no bump; restore runs guardOutranked, then sets deleted_at NULL, no bump. Bulk rows that do not apply are skipped and not counted in Affected; a refused row returns its ForbiddenError and rolls back the selection. Every refusal from a delete or an action is a ForbiddenError with Message set (UI-SPEC C8: the toast shows only the message).
- PartialData(ctx, "hint", record): a view {Kind, Title, Text} by precedence deleted (danger, user.trashed_hint_title and trashed_hint_desc) > deactivated (warning, user.deactivated_hint_title and deactivated_hint_desc) > the editor's own record (warning, user.self_hint_title and self_hint_desc) > nothing (empty Title renders nothing).
contracts.go: BackendUser FilterScopes adds filterTrashed: value hide narrows to `backend_users.deleted_at IS NULL`, only to `IS NOT NULL`, both or none no narrowing (accept the value shapes the analog's scope parser accepts). The controller's FilterOptions serves its two options (hide, only) labelled user.filter_deleted_hide and user.filter_deleted_only.
YAML and partial: _hint.htm copied verbatim from USER/controllers/users/_hint.htm; config_form.yaml preview headerPartial hint, recordActions [activate, deactivate, restore], messages deleteConfirm backend::lang.user.delete_confirm and deleted backend::lang.messages.form.deleted; config_list.yaml toolbar buttons [create, delete], showCheckboxes true, bulkActions [activate, deactivate, restore], messages empty backend::lang.user.list_empty, deleteConfirm backend::lang.user.delete_selected_confirm, deleted backend::lang.messages.list.deleted, rowStateDeleted backend::lang.user.state_deleted, rowStateDisabled backend::lang.user.state_deactivated (no rowStateNegative); config_filter.yaml gets the trashed scope (scope filterTrashed, modelClass `Backend\Models\User`, label backend::lang.user.filter_deleted).
admin:* commands: none clears is_superuser or deactivates today (commands.go:71-168), so D-09 needs no command change; leave commands.go and token_commands.go untouched (SC-1).
Smoke (append): `TestAdministratorsLifecycleSmoke`: the superuser deactivates another admin by record action and that admin's earlier bearer token answers 401 on GET /auth/me; deletes an admin through DELETE, finds the row with row state deleted in the list, restores it with the record action and it lists without the state; a bulk deactivate whose selection holds another admin and the actor's own row answers 403 with the self_deactivate text and leaves both rows activated (the whole selection rolled back).
Docs in this commit: docs/backend/users-and-permissions.md, a lifecycle paragraph (activate, deactivate, soft delete, restore, the trashed filter, the last active superuser rule on every path, revocation: deactivate and delete end the target's sessions and personal API tokens, activate and restore bring their personal tokens back, the admin:* commands never clear the superuser flag); cabana README Features. Commit: `feat(cabana): administrator lifecycle and last-superuser protection`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./modules/cabana/... &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>Non-zero exit from vet, the short suite, hello or the docs checker.</fails_when>
<automated>go test -count=1 -run '^(TestBackendScreensPostgresSmoke|TestAdministratorsFormSmoke|TestAdministratorsLifecycleSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>Fewer than 3 top-level "--- PASS: Test" lines: a smoke failed or skipped (Docker unreachable: rerun through sudo -u golem, else NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'guardLastSuperuser(' modules/cabana/backend_users_admin.go` prints at least 3 (update demotion, delete, deactivate).
- `grep -v '^\s*//' modules/cabana/backend_users_admin.go | grep -cE 'Unscoped\(\)\.Delete|Unscoped\(\)\.Where\([^)]*\)\.Delete'` prints 0.
- `grep -c 'revocationCutoff()' modules/cabana/backend_users_admin.go` prints at least 3 (password set, deactivate, delete).
- `grep -c 'bulkActions: \[activate, deactivate, restore\]' modules/cabana/backendscreens/controllers/users/config_list.yaml` prints 1 and `grep -c 'recordActions: \[activate, deactivate, restore\]' modules/cabana/backendscreens/controllers/users/config_form.yaml` prints 1.
- `grep -c 'filterTrashed' modules/cabana/backendscreens/controllers/users/config_filter.yaml` prints 1.
- `cmp modules/cabana/backendscreens/controllers/users/_hint.htm ../sm-grzybyfunkcjonalne-app/plugins/golem15/user/controllers/users/_hint.htm` exits 0.
- `git log --format=%h "$PLAN_BASE"..HEAD -- modules/cabana/commands.go modules/cabana/token_commands.go` prints nothing.
</acceptance_criteria>
<done>A superuser deactivates, deletes, restores and reactivates administrators from the preview and the list; access ends at once; the last active superuser survives every route.</done>
</task>
<task type="auto">
<name>Task 4: Roles screen: superusers and holders of backend.manage_roles create and edit roles with the permission editor; system roles keep their code and no role in use is deleted</name>
<reversibility rating="reversible">A cabana controller and YAML; role rows keep the existing schema.</reversibility>
<files>modules/cabana/backend_roles_admin.go, modules/cabana/backend_screens.go, modules/cabana/backendscreens/controllers/roles/config_list.yaml, modules/cabana/backendscreens/controllers/roles/config_form.yaml, modules/cabana/backendscreens/models/userrole/columns.yaml, modules/cabana/backendscreens/models/userrole/fields.yaml, modules/cabana/backend_screens_smoke_test.go, modules/cabana/README.md, docs/backend/users-and-permissions.md</files>
<read_first>modules/cabana/backend_screens.go, backend_users_admin.go and backend_privileges.go (from Tasks 1-3), modules/cabana/contracts.go (:17-28), modules/cabana/registry.go (:440-460), modules/lagoon/backend_admin_migrations.go (:20-70), modules/cabana/commands.go (:185-210 roleIDByCode), modules/phrasebook/translator.go (:150-195), USER/controllers/usergroups_admin_controller.go, USER/controllers/usergroups/config_list.yaml, USER/controllers/usergroups/config_form.yaml, USER/controllers/users_admin_controller.go (:479-536), ../examples/golem15-wintercms-starter/modules/backend/controllers/UserRoles.php, ../examples/golem15-wintercms-starter/modules/backend/models/UserRole.php, ../examples/golem15-wintercms-starter/modules/backend/models/userrole/fields.yaml, 19.6.1-UI-SPEC.md (C1, C5, C8 and the D-17 note), 19.6.1-RESEARCH.md (Pattern 6, Pitfall 5)</read_first>
<action>Per D-02 (Roles tile), D-07 (ceiling on the Roles screen), D-16 and D-17.
backend_roles_admin.go (new): `rolesAdmin{screens *backendScreens}`: ID RolesControllerID, ModelName `Backend\Models\UserRole`, ConfigDir controllers/roles, RequiredPermissions [PermissionManageRoles], NewRecord `&amp;BackendUserRole{}`, FormRecordTitle the role name.
- PermissionEditorProvider (mode checkbox): options are the catalog in permissionOrder with `Locked: !actsAsSuperuser(editor) &amp;&amp; !granted(editor.PermissionGrants, code)`, so a non-superuser can neither add nor remove a permission they lack on any role (D-07); AdminPermissionValues decodes the role's Permissions JSON (value 1 only); AdminSetPermissionValues stores compact JSON, `{}` when empty.
- A system role is `IsSystem` or a role whose stored code has code-declared grants (`len(reg.rolePermissions(code)) &gt; 0`).
- FormBeforeCreate and FormBeforeUpdate: a code already used by another role (exact match, excluding the record's own id) answers 422 on code with backend::lang.user.role.code_taken (the database has only an index on code and roleIDByCode fails on an ambiguous one). FormBeforeUpdate re-reads the stored code and is_system through a fresh session on the tx (code is fillable and Fill has already overwritten it; USER :479-497 pattern); a system role whose code changed answers 422 on code with backend::lang.user.role.code_locked (D-16). is_system is a protected key and never fillable, so roles created here are never system roles.
- FormBeforeDelete: a system role answers 403 with Message backend::lang.user.role.delete_system; otherwise count backend_users holding role_id unscoped (soft-deleted admins still reference the role, the foreign key has no ON DELETE); a count above zero answers 403 with Message set to the translator's Choice of backend::lang.user.role.delete_has_users for that count with name and count params in the request locale (a ForbiddenError carries no params, so the hook translates; phrasebook Translator from the app, towel locale on ctx). Roles have no deleted_at, so the generic delete is a hard delete, which is correct for an unused, non-system role.
backend_screens.go: AdminControllers adds rolesAdmin; Settings adds the link entry Code roles, Controller RolesControllerID, Permissions [PermissionManageRoles], Label backend::lang.user.role.menu_label, Description backend::lang.user.role.menu_description, Category backend::lang.settings.category_system, Icon shield-check, Order 110 (D-02, UI-SPEC C1).
YAML (UI-SPEC C5, shapes from USER/controllers/usergroups/): controllers/roles/config_list.yaml (list models/userrole/columns.yaml, modelClass `Backend\Models\UserRole`, title backend::lang.user.role.list_title, recordUrl summercms/cabana/roles/update/:id, showCheckboxes true, noRecordsMessage backend::lang.user.role.list_empty, toolbar buttons [create, delete] with the search prompt, no bulk actions, messages create backend::lang.user.role.new, deleteConfirm backend::lang.user.role.delete_selected_confirm, deleted backend::lang.messages.list.deleted); controllers/roles/config_form.yaml (name backend::lang.user.role.name, form models/userrole/fields.yaml, create redirect summercms/cabana/roles/update/:id and redirectClose summercms/cabana/roles, update redirect to the update screen and redirectClose to the list, no preview block, messages create user.role.new, update user.role.edit, saved, deleteConfirm user.role.delete_confirm, deleted); models/userrole/columns.yaml (name and code searchable and sortable, description searchable, is_system switch sortable, created_at and updated_at invisible sortable); models/userrole/fields.yaml (name left with comment user.role.name_comment; code right with `preset: {type: slug, field: name}` and comment user.role.code_comment; description textarea size tiny span full; permissions on tab backend::lang.user.permissions, permissioneditor mode checkbox, label user.role.permissions_field, comment user.role.permissions_comment, span full).
Smoke (append): `TestRolesSmoke`: the superuser creates a role (name and code, one permission) and gets 201; assigns it to an admin through the Administrators update; deleting the role answers 403 whose message contains the role name and 1; deleting the seeded developer role answers 403 with the delete_system text; changing developer's code answers 422 on code; GET /settings for the superuser lists administrators before roles.
Docs in this commit: docs/backend/users-and-permissions.md, a Roles paragraph (Settings → System → Roles, system roles and their code, code-declared grants merged at runtime, the D-07 ceiling, the in-use and system-role delete refusals); cabana README Features. Commit: `feat(cabana): Roles screen`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check &amp;&amp; scripts/check-admin-openapi.sh --check</automated>
<fails_when>Non-zero exit from vet, the whole short suite, hello, the docs checker, or the OpenAPI drift check (this plan must not change any swag-visible type).</fails_when>
<automated>go test -count=1 -run '^(TestBackendScreensPostgresSmoke|TestAdministratorsFormSmoke|TestAdministratorsLifecycleSmoke|TestRolesSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 4 &amp;&amp; go test -count=1 ./modules/cabana ./modules/surf</automated>
<fails_when>Fewer than 4 top-level "--- PASS: Test" lines (a smoke failed or skipped; Docker unreachable means NOT RUN after a sudo -u golem retry), or the full Docker-backed cabana and surf suites exit non-zero.</fails_when>
</verify>
<acceptance_criteria>
- `grep -c '"summercms.cabana.roles"' modules/cabana/backend_screens.go` prints 1 and `grep -c 'Code: *"roles"' modules/cabana/backend_screens.go` prints 1.
- `grep -c 'mode: checkbox' modules/cabana/backendscreens/models/userrole/fields.yaml` prints 1 and `grep -c 'type: slug' modules/cabana/backendscreens/models/userrole/fields.yaml` prints 1.
- `grep -c 'delete_has_users' modules/cabana/backend_roles_admin.go` prints at least 1 and `grep -c 'Choice(' modules/cabana/backend_roles_admin.go` prints at least 1.
- `grep -c 'code_locked' modules/cabana/backend_roles_admin.go` prints at least 1.
- `grep -c 'preview:' modules/cabana/backendscreens/controllers/roles/config_form.yaml` prints 0.
- `git log --format=%h "$PLAN_BASE"..HEAD -- modules/cabana/commands.go modules/cabana/token_commands.go modules/cabana/account.go` prints nothing.
</acceptance_criteria>
<done>Roles are listed, created, edited and deleted on their own Settings tile; system role codes stay fixed; a role in use or a system role cannot be deleted; a limited editor cannot widen any role.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| admin SPA / API client → cabana admin API | Request bodies, ids and query filters are untrusted; the principal comes from the JWT or personal token guard |
| admin with backend.manage_users → other admins' accounts | A delegated, non-superuser administrator acts on accounts that may hold more privilege |
| contributing plugin → another controller's screen (D-14) | Plugin code runs inside the target's transaction behind the target's permission |
| concurrent write transactions → last-superuser invariant | Two requests may reduce the superuser set at once |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6.1-01 | Elevation | generic CRUD fill of is_superuser, role_id, permissions, is_activated, password | high | mitigate | protectedFillKey keeps them unfillable; only usersAdmin hooks, the radio editor provider and the WritableForeignKey role relation write them, each after its check |
| T-19.6.1-02 | Elevation | FormBeforeCreate/Update virtual is_superuser | critical | mitigate | presence check on VirtualFieldsFromContext: a non-superuser sending the key answers 403 superuser_forbidden even when the field is hidden (D-05) |
| T-19.6.1-03 | Elevation | own record: role, superuser, permissions, activation | high | mitigate | priorRoleID snapshot compared in FormAfterUpdate (rollback), AdminSetPermissionValues self refusal, self checks on is_superuser, deactivate and delete (D-08) |
| T-19.6.1-04 | Elevation | permission editors and role picker | high | mitigate | PermissionOption.Locked for codes the editor lacks (stored value kept, change 403) and RelationLockProvider on role ids whose roleGrantSet is not covered (D-07); conservative wildcard rule in grantsCovered |
| T-19.6.1-05 | Elevation | password set, deactivate, delete, actions on a higher-privileged admin | critical | mitigate | guardOutranked on update, delete, record and bulk actions for non-superusers (D-19); D-20 refuses an own-password set without the current password |
| T-19.6.1-06 | Denial of service | demote, deactivate, delete of the last active superuser | high | mitigate | guardLastSuperuser on every reducing path: pg_advisory_xact_lock after the row lock plus a count of other active superusers in the write tx (D-09) |
| T-19.6.1-07 | Information disclosure | list, show, partials, filters for non-superusers | medium | mitigate | ListExtendQuery/FormExtendQuery filter on backend_users.is_superuser (404 by id) and hiddenSchemaFields drops the field, column and filter (D-03) |
| T-19.6.1-08 | Elevation | superuser's scoped personal API token | high | mitigate | actsAsSuperuser requires an unscoped or full token scope for every superuser-only power on these screens |
| T-19.6.1-09 | Elevation | contributed record actions (D-14) | high | mitigate | protect() checks the target's RequiredPermissions before the action's own Permissions; compileContributions validates contributed permissions at boot; the run path is the existing row-locked RecordAction |
| T-19.6.1-10 | Tampering | contributed action names | medium | mitigate | boot error on a name the owner or an earlier contributor registers, on reserved names, nil Run, empty label and unknown controller ID |
| T-19.6.1-11 | Spoofing | sessions after deactivate, delete, password set | high | mitigate | tokens_valid_after = now+1s on all three (D-12); FindByID refuses inactive and deleted users for personal tokens |
| T-19.6.1-12 | Spoofing | personal API tokens after a password set for another admin | medium | accept | same semantics as admin:reset-password, documented; the operator revokes them with admin:token:revoke; revoking would widen the phase (flagged) |
| T-19.6.1-13 | Tampering | unprojected is_superuser switch | high | mitigate | extendRecordData projects the stored flag to superusers so an untouched save never demotes (Pitfall 1) |
| T-19.6.1-14 | Elevation | upgrade of existing installs | medium | mitigate | the two codes are declared with no role grants (D-04); only superusers reach the screens until a grant is made |
| T-19.6.1-15 | Denial of service | role delete with holders | low | mitigate | unscoped holder count refuses with 403 before the FK would fail (D-17) |
| T-19.6.1-16 | Information disclosure | login/email collision messages revealing deleted accounts | low | accept | only holders of backend.manage_users see them, and they can list deleted admins anyway |
| T-19.6.1-17 | Denial of service | oversized passwords | low | mitigate | 72-byte cap before bcrypt, min 4, confirmation (D-11) |
| T-19.6.1-SC | Tampering | npm/pip/cargo installs | high | mitigate | this plan installs no package; any install would need the package-legitimacy gate and a blocking human checkpoint |
</threat_model>
<verification>
- After each task: `go vet ./... && go test -short ./modules/cabana/... ./modules/pact/... ./modules/surf/...`, `go -C examples/hello test ./...`, the task's smoke tests with Docker, `go test -run '^TestDocsTree$' ./cmd/summer`, `go run ./cmd/summer docs:build --check`.
- After Task 4: `go test ./modules/cabana ./modules/surf` with Docker, `go test -short ./...`, `scripts/check-admin-openapi.sh --check` (no swag-visible change in this plan).
- SC-1: `git log "$PLAN_BASE"..HEAD -- modules/cabana/commands.go modules/cabana/token_commands.go modules/cabana/account.go` is empty; the existing TestAdmin*, TestAccount* and conformance tests pass unchanged.
- Manual (verify-work UAT, VALIDATION Manual-Only): sign in as a superuser and as a limited admin, open Settings → System → Administrators and Roles, create, edit, deactivate.
</verification>
<success_criteria>
- Both screens work end to end for superusers; every D-03..D-12, D-16, D-17, D-19, D-20 refusal answers with the status and copy of UI-SPEC C8.
- A plugin can contribute a record action to summercms.cabana.users or any other controller; the boot errors fire.
- Admin-less applications and every existing test keep their behaviour; docs and READMEs describe the new surface.
</success_criteria>
<output>
Create `.planning/phases/19.6.1-backend-administrators-screen/19.6.1-01-SUMMARY.md` when done
</output>

View File

@@ -0,0 +1,293 @@
---
phase: 19.6.1-backend-administrators-screen
plan: 02
type: execute
wave: 2
depends_on: ["19.6.1-01"]
files_modified:
- modules/cabana/field_permission.go
- modules/cabana/relation_field.go
- modules/cabana/crud.go
- modules/cabana/backend_roles_admin.go
- modules/cabana/backend_users_admin.go
- modules/cabana/field_trigger.go
- modules/cabana/form_schema.go
- modules/cabana/schema_types.go
- modules/cabana/admin_openapi.go
- modules/cabana/backendscreens/models/user/fields.yaml
- modules/cabana/record_locks_smoke_test.go
- modules/cabana/field_trigger_smoke_test.go
- modules/cabana/README.md
- modules/phrasebook/backend/lang/en/lang.yaml
- modules/phrasebook/backend/lang/pl/lang.yaml
- admin/openapi/admin.json
- admin/src/api/schema.d.ts
- admin/src/api/types.ts
- admin/src/components/form/formContext.ts
- admin/src/components/form/control.ts
- admin/src/components/form/formState.ts
- admin/src/components/form/trigger.ts
- admin/src/components/form/FormField.vue
- admin/src/components/form/fields/PermissionEditorField.vue
- admin/src/views/FormView.vue
- admin/src/views/PreviewView.vue
- modules/boardwalk/dist
- docs/backend/forms.md
autonomous: true
requirements: [SC-1, SC-2, D-06, D-08, D-16, D-18, D-21, D-22]
estimate:
tokens: 190000
raw_tokens: 190000
tasks: 2
confidence: low
must_haves:
truths:
- "D-21, D-16: on a role that a code-declared pact.Permission.Roles grant attaches to, the Roles form shows each such grant checked, disabled, with the Lock icon and the note backend::lang.permissioneditor.locked_by_code; the save body never carries those codes, the server keeps them, and the save does not answer 403 even when the role's stored JSON lacks them."
- "D-21, D-08: on the editor's own Administrators record every permission override row is record-locked with its stored value (1, -1 or inherit) and the note backend::lang.permissioneditor.locked_self, the save body omits them, and a hand-crafted body that changes one answers 403 on permissions."
- "D-21: show and save responses carry meta.permissionLocks keyed by permissioneditor field name with entries {code, value, note} (note translated in the request locale) only when a record lock applies; the form schema never carries it; responses of controllers without a lock provider are unchanged (omitempty)."
- "D-21, D-06: fields.yaml accepts trigger with exactly action (disable or hide), field and condition (checked or unchecked); a missing or extra sub-key, another value, a controlling field that is not in the same form, is the field itself or is not a switch or checkbox, and a trigger in a repeater row, settings or relation form stop boot naming the controller, the field and the key; the form schema carries it as trigger."
- "D-06: on the Administrators form a superuser opening another superuser sees Permission overrides read-only with the note “Not editable while “Superuser” is on.” between the label and the control, the field is left out of the save body while disabled and the server keeps the stored overrides; switching Superuser off makes it editable at once; a non-superuser, whose schema has no Superuser switch, always gets an editable editor (subject to D-07 locks)."
- "D-21: trigger hide removes the field from rendering, from the save body and from the tab computation (a tab whose fields are all hidden disappears and the first remaining tab becomes active), and a server message for a hidden field appears in the FormErrorBanner as an orphan line; PreviewView applies hide only."
- "D-21: a user edit that flips a trigger to active announces “<field label>: <note>” in the form's polite live region; enabling announces nothing; values typed before a field was disabled stay in the form and return when it is enabled."
- "D-21, D-18: admin/openapi/admin.json and admin/src/api/schema.d.ts are regenerated in the same commits that change cabana.FormField and cabana.RecordMeta (scripts/check-admin-openapi.sh --check passes after each), and modules/boardwalk/dist is rebuilt from the committed lockfile and committed (scripts/check-admin-dist.sh passes)."
- "D-21: server enforcement stays authoritative: a client that ignores trigger and record locks gains nothing, because Plan 01's rules and the record-lock save rule decide every write."
- "UI: the trigger note sits above a long permission editor so it is visible without scrolling; a superuser target's Permissions tab shows the Superuser card on, then the read-only overrides editor with the note."
- "UI: record locks render as: none → the plain editor; some → those rows locked with the record's note and the rest editable; all (your own record) → the whole editor read-only; a row both request-locked and record-locked shows the record lock's value and note; on read-only screens (preview, trigger-disabled editor) record locks show the value only, without icon or note."
- "UI: an invalid trigger is a boot error and never reaches the SPA; the trigger and lock notes are static text with no loading state."
artifacts:
- path: "modules/cabana/field_trigger.go"
provides: "FieldTrigger type, trigger compile and boot checks"
contains: "type FieldTrigger struct"
- path: "modules/cabana/field_permission.go"
provides: "PermissionRecordLock, PermissionRecordLockProvider and the record-lock save rule"
contains: "type PermissionRecordLockProvider interface"
- path: "modules/cabana/relation_field.go"
provides: "RecordMeta.PermissionLocks"
contains: "permissionLocks,omitempty"
- path: "admin/src/components/form/trigger.ts"
provides: "trigger evaluation helpers"
contains: "export function applyTriggers"
- path: "admin/src/components/form/fields/PermissionEditorField.vue"
provides: "record-locked rows"
contains: "data-locked-by"
- path: "modules/boardwalk/dist"
provides: "committed admin build with the D-21 SPA support"
key_links:
- from: "modules/cabana/crud.go"
to: "modules/cabana/field_permission.go"
via: "projectFullRecord fills meta.permissionLocks from the controller's PermissionRecordLockProvider"
pattern: "AdminPermissionRecordLocks\\("
- from: "admin/src/views/FormView.vue"
to: "admin/src/components/form/trigger.ts"
via: "the effective field list (tabs, grid, banner names, payload) comes from applyTriggers"
pattern: "applyTriggers\\("
- from: "admin/src/components/form/formState.ts"
to: "admin/src/components/form/control.ts"
via: "editablePayload passes record-locked codes to permissionValues so they are never sent"
pattern: "permissionValues\\("
- from: "modules/cabana/backendscreens/models/user/fields.yaml"
to: "modules/cabana/field_trigger.go"
via: "the permissions field declares trigger disable on is_superuser checked"
pattern: "field: is_superuser"
prohibitions:
- statement: "The SPA never becomes the enforcement point: a trigger-disabled field or a record-locked code is never trusted from the client; the server keeps stored values and decides every write."
status: unresolved
verification: judgment
- statement: "No new npm dependency, design token, colour, font, radius, shadow or icon outside the existing set is added, and no new .vue file is needed."
status: unresolved
verification: judgment
- statement: "Trigger and lock notes render through text interpolation only, never v-html."
status: unresolved
verification: judgment
- statement: "Payloads of controllers that use neither trigger nor record locks are unchanged: the new keys are omitempty and existing keys keep their meaning."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not a user story; MVP mode off): cabana gets WinterCMS's Administrators screens, managed in the admin SPA, with the privilege rules on the server.
This plan's slice (plan 2 of 3, wave 2, D-22 "a short plan for the SPA support in D-21"): the two display features D-21 put in scope, end to end. The tracer carries per-record permission locks from a controller provider through the record response to the permission editor (D-16 code-declared role grants, D-08 your own overrides). Task 2 adds the field `trigger` (D-06: overrides disabled while Superuser is on) and the committed SPA build. Each feature lands with the server type, the swag-generated OpenAPI and `schema.d.ts`, the SPA code and the docs in one commit, so no commit leaves generated files stale.
## Spec-less probe fallback
Skipped for edges (no requirement IDs, no SPEC.md; visible choice). The UI-SPEC "UI Considerations" rows for the trigger and the record locks are lifted into `must_haves.truths` (lines starting "UI:"); the backstop rows for these features are owned by Plan 03, which writes those tests. Prohibitions recalled by hand, descriptor-less.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- Partition (D-22): Plan 01 changed no swag-visible type; this plan owns every change to `cabana.FormField` and `cabana.RecordMeta`, so the OpenAPI regeneration happens here, in the same commits as the type changes.
- Record locks are an exported, optional cabana interface next to `PermissionEditorProvider` (`PermissionRecordLockProvider`), so any plugin's permission editor can use them; UI-SPEC C7's recommended payload shape `meta.permissionLocks: {field: [{code, value, note}]}` is kept as is.
- Save rule for a record-locked code: absent from the body keeps the stored value; present with the stored value is accepted; present with another value answers 403 on the field (the existing locked phrase). This works for the old SPA (which sends stored values) and the new one (which sends none), and keeps D-08's "the server refuses" for hand-crafted bodies.
- Locks travel to the permission editor through a provided injection key (`FORM_PERMISSION_LOCKS`) rather than prop threading through FormGrid, FormField and FieldRenderer (UI-SPEC C7 leaves the path to the planner); trigger notes travel the same way (`FORM_TRIGGER_NOTES`).
- Trigger is evaluated only where the SPA renders controller forms (FormView, PreviewView), so the server refuses it in repeater rows, settings forms and relation forms.
- `trigger` supports only the `checked` and `unchecked` conditions (Winter's `value[...]` is out of scope, UI-SPEC C6).
- The dist is rebuilt once, in Task 2, after both features; Task 1 leaves `modules/boardwalk/dist` stale for one commit (the release gate applies before tagging, `.planning/notes/2026-10-07-embedded-admin-release-gate.md`).
## Artifacts this phase produces (this plan)
- cabana exported: `type PermissionRecordLock struct { Code string; Value int; Note string }` (json code, value, note), `type PermissionRecordLockProvider interface { AdminPermissionRecordLocks(ctx context.Context, field string, record any) ([]PermissionRecordLock, error) }`, `RecordMeta.PermissionLocks map[string][]PermissionRecordLock` (json `permissionLocks,omitempty`), `type FieldTrigger struct { Action, Field, Condition string }` (json action, field, condition), `FormField.Trigger *FieldTrigger` (json `trigger,omitempty`).
- cabana unexported: `compileTrigger`, `checkTriggers`; record-lock handling in `applyPermissionValues` and `projectFullRecord`; `AdminPermissionRecordLocks` on `rolesAdmin` and `usersAdmin`.
- fields.yaml key `trigger` (`action: disable|hide`, `field`, `condition: checked|unchecked`).
- Lang keys `backend::lang.form.trigger_disabled_on`, `form.trigger_disabled_off`, `permissioneditor.locked_by_code`, `permissioneditor.locked_self` (en, pl).
- SPA: `admin/src/components/form/trigger.ts` (`triggerState`, `applyTriggers`), injection keys `FORM_PERMISSION_LOCKS` and `FORM_TRIGGER_NOTES` in formContext.ts, `permissionValues(field, value, locked?)`, `editablePayload(fields, values, mode, locks?)`, type aliases `PermissionRecordLock` and `FieldTrigger` in admin/src/api/types.ts; regenerated `admin/openapi/admin.json`, `admin/src/api/schema.d.ts`; rebuilt `modules/boardwalk/dist`.
- Smoke tests `TestPermissionRecordLocksSmoke`, `TestFieldTriggerSmoke` (-short).
<objective>
Ship the SPA support D-21 put in scope, server type to rendered control: per-record locked permission values (D-16 code-declared role grants, D-08 your own overrides) and the field `trigger` (D-06), with the OpenAPI and `schema.d.ts` regenerated and the committed admin build refreshed (D-22 plan 02).
Purpose: D-06, D-08, D-16 display rules via D-21; SC-1 (roles shown with the permission editor as they really are); server stays authoritative (SC-2).
Output: two framework commits, each with server, generated OpenAPI, SPA and docs; the second also commits `modules/boardwalk/dist`.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-CONTEXT.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-UI-SPEC.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-PATTERNS.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-01-SUMMARY.md
@.planning/notes/2026-10-07-embedded-admin-release-gate.md
Concurrency and commits: as in Plan 01 (record `PLAN_BASE=$(git rev-parse HEAD)` before Task 1; check `git status --porcelain -- modules/cabana admin modules/boardwalk docs` before each task; stage by explicit path; no co-author tags; docs with the code; `go vet ./...`, `go test -short ./...` and `go -C examples/hello test ./...` green at every commit). `npm --prefix admin ci` installs from the committed lockfile only; no `npm install` of new packages. When staging `modules/boardwalk/dist`, stage the whole directory path (hashed asset names change) and nothing else from other sessions.
<interfaces>
- From Plan 01 (19.6.1-01-SUMMARY.md has the names as built): `rolesAdmin`, `usersAdmin`, `actsAsSuperuser`, `Registry.permissionOrder`, `Registry.rolePermissions(code)`, `PermissionManageUsers`, `AdministratorsControllerID`, `RolesControllerID`; the users fields.yaml Permissions tab with `is_superuser` (virtual switch, hidden for non-superusers by `hiddenSchemaFields`) and `permissions` (permissioneditor, radio).
- cabana: `PermissionOption`, `PermissionEditorProvider`, `permissionLockedKey`, `localizePermissionOptions`, `liftPermissionValues`, `applyPermissionValues` (the locked comparison loop ~:303-307), `projectPermissionFields` field_permission.go; `RecordMeta{Title, Labels, Actions, Notices}` relation_field.go:95-102; `projectFullRecord` crud.go:647 and the show and save paths that localize meta with `s.tr` (offeredRecordActions, `localizeNotices`); `translateKey(ctx, tr, key)`; `formFieldKeys` form_schema.go:38-50, `compileDependsOn` call form_schema.go:646, `compileDependsOn` / `checkDependsOn` field_dependent.go:23-60 (the compile and sibling-check analog), `FormField` schema_types.go:260-300 (`DependsOn` json `dependsOn,omitempty`); settings forms compile in settings.go `compileSettingWithTypes` (refuses plugin field types: mirror that refusal), repeater row forms in field_repeater.go, relation forms in relation_form.go; swag descriptions of the form schema and record responses in admin_openapi.go (the `preset` / `dependsOn` sentences reflected in admin.json :5261).
- SPA: `FormField`, `PermissionOption`, `RecordMeta` aliases admin/src/api/types.ts:36-51; `permissionValues(field, value)` control.ts:77-90 and `toggleOn` (control.ts, above :60); `editablePayload(fields, values, mode)` formState.ts:53-77 (skips readOnly fields; permissioneditor through permissionValues); FormView.vue: `fields` computed :117 (context filter), `fieldNames` :124, `tabs` :126, `panelFields`, `labels` :80 set from `record.meta.labels` :223, snapshot and save `editablePayload` :208, :226, :398, `announce` :324, provides :314-349, FormErrorBanner :577; PreviewView.vue: `fields` :71, `tabs` :84, meta handling :152, provides :210; formContext.ts injection keys (FORM_VALUES and others, defaults for forms without a provider); FormField.vue (label, FieldRenderer, comment, error markup; describedBy); PermissionEditorField.vue (`disabled(option)` :79, `showsLock(option)` :83, `valueOf`, lock icon and note markup ~:147-166, `t('backend::lang.permissioneditor.locked')`); RelationField.vue locked note markup :326-337 (`Lock :size="14" class="shrink-0 text-muted"`).
- Scripts: `scripts/check-admin-openapi.sh [--check]` (swag v1.16.6 → swagger2openapi → openapi-typescript), `scripts/check-admin-dist.sh` (typecheck, vite build into a temp dir, diff with modules/boardwalk/dist), `npm --prefix admin run test|typecheck|build`.
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: a role that a plugin's code-declared grant attaches to shows that grant checked and locked with its note on the Roles form, and your own Administrators record shows every override locked, with saves keeping the locked values</name>
<reversibility rating="costly">meta.permissionLocks and PermissionRecordLockProvider become a published payload and interface that plugins and the SPA read; changing them later needs coordinated SPA and plugin changes.</reversibility>
<files>modules/cabana/field_permission.go, modules/cabana/relation_field.go, modules/cabana/crud.go, modules/cabana/backend_roles_admin.go, modules/cabana/backend_users_admin.go, modules/cabana/admin_openapi.go, modules/cabana/record_locks_smoke_test.go, modules/phrasebook/backend/lang/en/lang.yaml, modules/phrasebook/backend/lang/pl/lang.yaml, admin/openapi/admin.json, admin/src/api/schema.d.ts, admin/src/api/types.ts, admin/src/components/form/formContext.ts, admin/src/components/form/control.ts, admin/src/components/form/formState.ts, admin/src/components/form/fields/PermissionEditorField.vue, admin/src/views/FormView.vue, admin/src/views/PreviewView.vue, modules/cabana/README.md, docs/backend/forms.md</files>
<read_first>.planning/phases/19.6.1-backend-administrators-screen/19.6.1-01-SUMMARY.md, modules/cabana/field_permission.go, modules/cabana/relation_field.go (:80-110), modules/cabana/crud.go (:500-560, :640-670), modules/cabana/backend_roles_admin.go, modules/cabana/backend_users_admin.go, modules/cabana/admin_openapi.go (record response and form schema descriptions), scripts/check-admin-openapi.sh, admin/src/api/types.ts, admin/src/components/form/formContext.ts, admin/src/components/form/control.ts, admin/src/components/form/formState.ts, admin/src/components/form/fields/PermissionEditorField.vue, admin/src/views/FormView.vue, admin/src/views/PreviewView.vue, admin/tests/form/PermissionEditorField.test.ts, docs/backend/forms.md ("Permission editor" :305-373), 19.6.1-UI-SPEC.md (C7, Framework additions copy)</read_first>
<action>Per D-21 for D-16 and D-08.
Server, field_permission.go: exported `PermissionRecordLock{Code string json:"code"; Value int json:"value"; Note string json:"note"}` and the optional `PermissionRecordLockProvider` with `AdminPermissionRecordLocks(ctx context.Context, field string, record any) ([]PermissionRecordLock, error)`, documented: implemented by a controller that also implements PermissionEditorProvider; asked with the request principal (bouncer.User) for a stored record, never on create; Value is what the editor shows (1, -1, or 0 for inherit); Note is a phrase key or text. applyPermissionValues on update: ask the provider for the record's locks; for a record-locked code the submitted value, when present in the body, must equal the stored value, else `&amp;ForbiddenError{Details: {field: [permissionLockedKey]}}`; whether present or not, the stored value is carried into next unchanged (like a non-offered code) and the code is skipped in the request-Locked comparison, so a system role whose code grant is not in its stored JSON saves without a 403.
relation_field.go: `RecordMeta.PermissionLocks map[string][]PermissionRecordLock` json `permissionLocks,omitempty`, with a doc comment (show and save responses; absent when no lock applies; never in the form schema). crud.go: projectFullRecord fills it for each permissioneditor field whose provider returns locks; the Note is translated in the request locale where the show and save responses localize their other meta (with `s.tr`, as for record action labels and notices).
backend_roles_admin.go: AdminPermissionRecordLocks returns, for the role's stored code, every code in `reg.rolePermissions(code)` in permissionOrder with Value 1 and Note backend::lang.permissioneditor.locked_by_code (D-16). backend_users_admin.go: for the editor's own record (principal ID equals the record's), every offered code with Value from the stored overrides (1, -1, else 0) and Note backend::lang.permissioneditor.locked_self; none for other records (D-08 display; AdminSetPermissionValues keeps refusing a change).
Lang (en, pl): permissioneditor.locked_by_code and permissioneditor.locked_self, verbatim from UI-SPEC "Framework additions (D-21)".
admin_openapi.go: extend the record response descriptions with meta.permissionLocks (per permissioneditor field, entries code, value, note; what the editor does with them); run `scripts/check-admin-openapi.sh` to regenerate admin/openapi/admin.json and admin/src/api/schema.d.ts and commit both.
SPA (UI-SPEC C7; tokens and markup copied from existing components):
- types.ts: `export type PermissionRecordLock = Schemas['cabana.PermissionRecordLock']`.
- formContext.ts: `FORM_PERMISSION_LOCKS: InjectionKey&lt;Readonly&lt;Ref&lt;Record&lt;string, PermissionRecordLock[]&gt;&gt;&gt;&gt;`; injecting controls default to an empty map.
- control.ts: `permissionValues(field, value, locked?: ReadonlySet&lt;string&gt;)` skips record-locked codes (never sent, like non-offered codes).
- formState.ts: `editablePayload(fields, values, mode, locks?)` passes each permissioneditor field's locked codes to permissionValues; callers without locks are unchanged.
- FormView.vue: keep `record.meta.permissionLocks ?? {}` next to `labels` (reset on every load and after a save), provide it, and pass it to every editablePayload call (dirty snapshot and save). PreviewView.vue: provide it from the record meta.
- PermissionEditorField.vue: inject the locks for `field.name`; a record-locked row shows the lock's value (checkbox checked for 1; radio segment for 1, 0, -1), its control is disabled with aria-disabled="true", the existing Lock 14px follows the label and the existing lock-note line (same id and aria-describedby link) shows the lock's note instead of permissioneditor.locked or the option comment; the row gets `data-locked` and `data-locked-by="record"`; a record lock wins over a request lock (value and note); on a read-only screen record locks show the value only, without icon or note (existing showsLock rule); `set()` refuses record-locked codes.
Smoke (modules/cabana/record_locks_smoke_test.go, package cabana, -short): `TestPermissionRecordLocksSmoke` with an in-test controller implementing both providers (offers a.x and a.y, record-locks a.x at value 1 with a phrase note, stored {a.y: 1}): applyPermissionValues on update with a body without a.x keeps a.x out of next unchanged-stored and stores a.y as submitted; a body sending a.x: 1 when stored lacks it answers ForbiddenError on the field; a body equal to the stored values passes; with a.x also request-Locked and absent from the body there is no 403; projectFullRecord's meta carries permissionLocks for the field and omits the key for a controller without the provider. Keep existing admin tests green (`npm --prefix admin run test`); Plan 03 adds the SPA tests.
Docs in this commit: docs/backend/forms.md "Permission editor": per-record locks (PermissionRecordLockProvider, meta.permissionLocks, the save rule, how the SPA shows them, example: grants a plugin declares for a role); cabana README API rows for PermissionRecordLock, PermissionRecordLockProvider and RecordMeta.PermissionLocks. Commit: `feat(cabana,admin): per-record locked permission values`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./modules/cabana/... &amp;&amp; go test -count=1 -short -run '^TestPermissionRecordLocksSmoke$' -v ./modules/cabana 2&gt;&amp;1 | grep -q -- '^--- PASS: TestPermissionRecordLocksSmoke' &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; scripts/check-admin-openapi.sh --check &amp;&amp; npm --prefix admin run typecheck &amp;&amp; npm --prefix admin run test &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>Non-zero exit from vet, the short suite, hello, the OpenAPI drift check (committed admin.json or schema.d.ts differ from a fresh generation), the SPA typecheck or vitest run, or the docs checker; or no "--- PASS: TestPermissionRecordLocksSmoke" line.</fails_when>
<automated>go test -count=1 -run '^(TestRolesSmoke|TestAdministratorsFormSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2</automated>
<fails_when>Fewer than 2 top-level "--- PASS: Test" lines: Plan 01's role and form saves broke under the record-lock rule, or Docker was unreachable ("--- SKIP"; rerun through sudo -u golem, else NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'type PermissionRecordLockProvider interface' modules/cabana/field_permission.go` prints 1 and `grep -c 'permissionLocks,omitempty' modules/cabana/relation_field.go` prints 1.
- `grep -c 'AdminPermissionRecordLocks(' modules/cabana/backend_roles_admin.go` prints 1 and the same count on modules/cabana/backend_users_admin.go prints 1.
- `grep -c 'locked_by_code' modules/phrasebook/backend/lang/pl/lang.yaml` prints 1 and `grep -c 'locked_self' modules/phrasebook/backend/lang/en/lang.yaml` prints 1.
- `grep -c 'permissionLocks' admin/src/api/schema.d.ts` prints at least 1 and `grep -c 'cabana.PermissionRecordLock' admin/openapi/admin.json` prints at least 1.
- `grep -c 'data-locked-by' admin/src/components/form/fields/PermissionEditorField.vue` prints at least 1 and `grep -c 'FORM_PERMISSION_LOCKS' admin/src/components/form/formContext.ts` prints 1.
- `grep -c 'v-html' admin/src/components/form/fields/PermissionEditorField.vue` prints 0.
- `git diff --name-only "$PLAN_BASE"..HEAD -- admin/package.json admin/package-lock.json` prints nothing after the commit (`PLAN_BASE` = `git rev-parse HEAD` recorded before Task 1).
</acceptance_criteria>
<done>Opening a system role shows its plugin-declared grants checked and locked with the note; your own record's overrides are locked; saving either keeps the locked values and never 403s on them, while a hand-crafted change is refused.</done>
</task>
<task type="auto">
<name>Task 2: Field trigger: on the Administrators form the permission overrides are read-only with a note while Superuser is on and editable again when it is off; then the committed admin build</name>
<reversibility rating="costly">The fields.yaml trigger key and its schema payload become a contract every plugin's YAML may use; reshaping it later touches plugin YAML and the SPA.</reversibility>
<files>modules/cabana/field_trigger.go, modules/cabana/form_schema.go, modules/cabana/schema_types.go, modules/cabana/admin_openapi.go, modules/cabana/backendscreens/models/user/fields.yaml, modules/cabana/field_trigger_smoke_test.go, modules/phrasebook/backend/lang/en/lang.yaml, modules/phrasebook/backend/lang/pl/lang.yaml, admin/openapi/admin.json, admin/src/api/schema.d.ts, admin/src/api/types.ts, admin/src/components/form/trigger.ts, admin/src/components/form/formContext.ts, admin/src/components/form/FormField.vue, admin/src/views/FormView.vue, admin/src/views/PreviewView.vue, modules/boardwalk/dist, modules/cabana/README.md, docs/backend/forms.md</files>
<read_first>modules/cabana/form_schema.go (:20-60, :600-700), modules/cabana/field_dependent.go (:1-80), modules/cabana/schema_types.go (:255-300), modules/cabana/settings.go (:70-140), modules/cabana/field_repeater.go (row form compile), modules/cabana/relation_form.go (form compile entry), modules/cabana/admin_openapi.go (form schema description), modules/cabana/backendscreens/models/user/fields.yaml, admin/src/components/form/control.ts (toggleOn), admin/src/components/form/formState.ts, admin/src/components/form/FormField.vue, admin/src/components/form/formContext.ts, admin/src/views/FormView.vue (:110-260, :314-420, :560-600), admin/src/views/PreviewView.vue (:60-120, :200-290), admin/src/components/form/FormErrorBanner.vue, admin/src/components/form/fields/RelationField.vue (:320-340), admin/tests/form/DependentDropdown.test.ts, docs/backend/forms.md (:111-172), scripts/check-admin-dist.sh, .planning/notes/2026-10-07-embedded-admin-release-gate.md, 19.6.1-UI-SPEC.md (C6, Framework additions copy, Typography and Spacing for the note)</read_first>
<action>Per D-21 for D-06.
Server: form_schema.go adds "trigger" to formFieldKeys and calls compileTrigger next to compileDependsOn. field_trigger.go (new): `FieldTrigger{Action string json:"action"; Field string json:"field"; Condition string json:"condition"}`; compileTrigger reads the `trigger` mapping: exactly the sub-keys action (disable or hide), field (an identifier) and condition (checked or unchecked); a missing or extra sub-key or another value is an error naming the key; it sets `FormField.Trigger`. checkTriggers(fields), run after the form's fields compile like checkDependsOn: the named field must be another field of the same form, of type switch or checkbox (a virtual switch such as is_superuser counts). A trigger inside a repeater row form, a settings form or a relation form is a boot error (the SPA evaluates triggers only in FormView and PreviewView); wire the refusal where those forms compile, as settings.go refuses plugin field types. Errors go through bootErr so they name the plugin, controller, file, field and key. schema_types.go: `Trigger *FieldTrigger` json `trigger,omitempty` on FormField with a doc comment. admin_openapi.go: add a sentence on trigger to the form schema description; regenerate admin/openapi/admin.json and admin/src/api/schema.d.ts with scripts/check-admin-openapi.sh. backendscreens/models/user/fields.yaml: the permissions field gets `trigger: {action: disable, field: is_superuser, condition: checked}` (D-06). Lang (en, pl): form.trigger_disabled_on and form.trigger_disabled_off with :field, verbatim from UI-SPEC.
SPA (UI-SPEC C6):
- trigger.ts (new): `triggerState(field, values, present: ReadonlySet&lt;string&gt;): 'active' | 'inactive'` (checked is active when toggleOn(values[controlling]); unchecked is active when it is off; a controlling field missing from `present`, the names of the schema fields that pass this request's filters and context, counts as off) and `applyTriggers(fields, values, labelOf): { fields, notes }`: a hide-active field is removed; a disable-active field becomes a copy with readOnly true; notes maps each disable-active field name to t(form.trigger_disabled_on or _off, {field: controlling label}). Pure, no Vue imports beyond types.
- formContext.ts: `FORM_TRIGGER_NOTES` injection key (default empty map).
- FormView.vue: apply applyTriggers to the context-filtered fields with the whole form's values (not the active tab's) and use the result everywhere the form uses fields: tabs, panelFields, fieldNames for FormErrorBanner (a hidden field's error becomes an orphan banner line), and every editablePayload call (a disabled field is readOnly, so it is not sent; a hidden one is absent), so the server keeps stored values. When the active tab disappears, the first remaining tab becomes active. Provide FORM_TRIGGER_NOTES. When a user edit turns a trigger active, call announce with "&lt;field label&gt;: &lt;note&gt;"; nothing on enable. Values stay in `values` while a field is disabled or hidden.
- FormField.vue: inject the notes; for a field with a note render, between the label and the control, `&lt;p :id="`${controlId}-trigger`" data-trigger-note class="flex items-center gap-2 text-[13px] text-muted"&gt;` holding Info (size 14, class shrink-0, aria-hidden) and the note text by interpolation, and add that id to describedBy.
- PreviewView.vue: apply hide triggers against the record values for fields and tabs; disable has no effect there and no note shows.
- types.ts: `export type FieldTrigger = Schemas['cabana.FieldTrigger']`.
Build: `npm --prefix admin run build`, then stage and commit modules/boardwalk/dist in this commit; scripts/check-admin-dist.sh must pass.
Smoke (modules/cabana/field_trigger_smoke_test.go, package cabana, -short): `TestFieldTriggerSmoke`: a fixture form (fstest.MapFS) with a switch and a field carrying a valid trigger compiles and its localized schema field carries Trigger; one invalid case per rule (unknown action, unknown condition, extra sub-key, missing field, self reference, a text controlling field) fails compilation with an error containing the field name and "trigger"; the compiled Administrators form (through withBackendScreens) carries the trigger on permissions. Keep `npm --prefix admin run test` green; Plan 03 adds the SPA tests.
Docs in this commit: docs/backend/forms.md, a "Triggers" section under field options (the key, the two actions and conditions, the boot errors, that the server stays authoritative and a disabled field is not sent); cabana README Features and the FieldTrigger API row. Commit: `feat(cabana,admin): field trigger and admin build`.</action>
<verify>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; go test -count=1 -short -run '^(TestFieldTriggerSmoke|TestPermissionRecordLocksSmoke|TestBackendScreensSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3 &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; scripts/check-admin-openapi.sh --check &amp;&amp; npm --prefix admin run test &amp;&amp; scripts/check-admin-dist.sh &amp;&amp; go test -count=1 -run '^TestDocsTree$' ./cmd/summer &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>Non-zero exit from vet, the whole short suite, hello, the OpenAPI drift check, vitest, the dist drift check ("modules/boardwalk/dist is stale"), or the docs checker; or fewer than 3 top-level "--- PASS: Test" lines among the three smokes.</fails_when>
<automated>go test -count=1 ./modules/cabana ./modules/surf</automated>
<fails_when>Non-zero exit of the Docker-backed cabana and surf suites (Plan 01 smokes included); a run where Docker was unreachable shows SKIP lines and is recorded NOT RUN after a sudo -u golem retry.</fails_when>
<human-check>Optional before verify-work: sign in as a superuser, open another superuser's Administrators form, Permissions tab: the overrides editor is read-only with the note; switch Superuser off and it becomes editable; open your own record and a system role and see the locked rows with their notes.</human-check>
</verify>
<acceptance_criteria>
- `grep -c '"trigger": {}' modules/cabana/form_schema.go` prints 1 and `grep -c 'type FieldTrigger struct' modules/cabana/field_trigger.go` prints 1.
- `grep -c 'trigger,omitempty' modules/cabana/schema_types.go` prints 1.
- `grep -c 'field: is_superuser' modules/cabana/backendscreens/models/user/fields.yaml` prints 1.
- `grep -c 'export function applyTriggers' admin/src/components/form/trigger.ts` prints 1 and `grep -c 'applyTriggers(' admin/src/views/FormView.vue` prints at least 1.
- `grep -c 'data-trigger-note' admin/src/components/form/FormField.vue` prints 1 and `grep -c 'v-html' admin/src/components/form/FormField.vue` prints 0.
- `grep -c 'trigger_disabled_on' modules/phrasebook/backend/lang/pl/lang.yaml` prints 1.
- `git status --porcelain -- modules/boardwalk/dist admin/openapi admin/src/api` prints nothing after the commit.
</acceptance_criteria>
<done>A superuser editing another superuser sees the overrides read-only with the note and editable once Superuser is off; plugins can declare triggers in fields.yaml; the committed admin build carries both D-21 features.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| admin SPA → cabana save API | The SPA's trigger and lock handling is a display aid; the body it sends is untrusted |
| controller lock provider → record response | Lock notes and values come from plugin code and reach every viewer of the record |
| plugin fields.yaml → boot compiler | A plugin's YAML may declare malformed triggers |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6.1-18 | Elevation | trigger-disabled permission overrides | medium | mitigate | the disabled field is omitted and the server keeps stored values; superusers bypass overrides anyway; no server rule depends on the trigger |
| T-19.6.1-19 | Elevation | record-locked codes in a hand-crafted save body | high | mitigate | applyPermissionValues refuses a present record-locked value that differs from the stored one (403) and carries the stored value otherwise; D-08's AdminSetPermissionValues refusal stays |
| T-19.6.1-20 | Information disclosure | meta.permissionLocks | low | accept | sent only on show and save responses behind protect() to admins who may open the record; it repeats codes the permission editor already shows |
| T-19.6.1-21 | Tampering | malformed trigger YAML | low | mitigate | compileTrigger and checkTriggers stop boot with errors naming controller, field and key |
| T-19.6.1-22 | Tampering | stale committed SPA build or OpenAPI | medium | mitigate | check-admin-openapi.sh --check and check-admin-dist.sh in this plan's verify; dist rebuilt from the committed lockfile |
| T-19.6.1-23 | Tampering | note text injected into markup | medium | mitigate | notes render by Vue text interpolation only (no v-html); acceptance greps forbid v-html in the touched components |
| T-19.6.1-SC | Tampering | npm/pip/cargo installs | high | mitigate | no package is added; `npm --prefix admin ci` installs only the committed lockfile; any new package would need the legitimacy gate and a blocking human checkpoint |
</threat_model>
<verification>
- Task 1: Go short suite and smoke, `scripts/check-admin-openapi.sh --check`, `npm --prefix admin run typecheck` and `test`, docs checker; Plan 01 role and form smokes with Docker.
- Task 2: the same plus `scripts/check-admin-dist.sh`, and the full Docker-backed cabana and surf suites.
- Manual (verify-work UAT): the human-check in Task 2.
</verification>
<success_criteria>
- Record locks and triggers work end to end on both screens; the server behaviour of Plan 01 is unchanged for clients that ignore them.
- Generated OpenAPI, schema.d.ts and the committed dist match fresh builds.
</success_criteria>
<output>
Create `.planning/phases/19.6.1-backend-administrators-screen/19.6.1-02-SUMMARY.md` when done
</output>

View File

@@ -0,0 +1,316 @@
---
phase: 19.6.1-backend-administrators-screen
plan: 03
type: execute
wave: 3
depends_on: ["19.6.1-01", "19.6.1-02"]
files_modified:
- modules/cabana/export_test.go
- modules/cabana/administrators_harness_test.go
- modules/cabana/administrators_postgres_test.go
- modules/cabana/administrators_lifecycle_postgres_test.go
- modules/cabana/backend_privileges_internal_test.go
- modules/cabana/backend_screens_test.go
- modules/cabana/action_extensions_test.go
- modules/cabana/action_extensions_postgres_test.go
- modules/cabana/roles_postgres_test.go
- modules/cabana/field_trigger_test.go
- modules/cabana/record_locks_test.go
- admin/tests/form/trigger.test.ts
- admin/tests/form/PermissionEditorField.test.ts
- admin/tests/form/formState.test.ts
- admin/tests/form/FormView.test.ts
- admin/tests/form/PreviewView.test.ts
- admin/tests/browser/record-actions-wrap.test.ts
- scripts/check-phase19.6.1.sh
- .planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md
autonomous: true
requirements: [SC-1, SC-2, SC-3, D-03, D-04, D-05, D-06, D-07, D-08, D-09, D-10, D-11, D-12, D-14, D-15, D-16, D-17, D-18, D-19, D-20, D-21, D-22]
estimate:
tokens: 280000
raw_tokens: 280000
tasks: 4
confidence: low
must_haves:
truths:
- "D-18, D-05: TestAdministratorsSuperuserFlag proves through the assembled HTTP handler that a non-superuser with backend.manage_users sending is_superuser true or false on create or update gets 403 with the superuser_forbidden text on is_superuser and the row is unchanged, and that a superuser grants and removes the flag."
- "D-18: the Postgres tests TestAdministratorsSuperuserHidden (D-03), TestAdministratorsSuperuserProjected (Pitfall 1), TestAdministratorsPermissionCeiling (D-06, D-07), TestAdministratorsRoleCeiling (D-07), TestAdministratorsSelfEdit (D-08), TestAdministratorsPasswordAndIdentity (D-11), TestAdministratorsOutranked (D-19) and TestAdministratorsOwnPassword (D-20) pass, each asserting status, the localized refusal text and that a refused write changed no row."
- "D-09: TestAdministratorsLastSuperuser races superusers A and B (the only active superusers) on each reducing path (update demotion, record deactivate, bulk deactivate, form delete, bulk delete): a test transaction holds the advisory lock until pg_locks shows both requests waiting, then releases; exactly one request succeeds, the other answers 403 with the last_superuser text, exactly one of A and B stays an active superuser, and no response is a 500 or names a deadlock."
- "D-09: TestAdministratorsLastSuperuserConcurrent runs ten unsynchronized rounds of A demoting B while B demotes A and ends every round with at least one active superuser and no 500."
- "D-10, D-12: TestAdministratorsLifecycle covers activate, deactivate and restore as record and bulk actions, soft delete with the deleted row state and the trashed filter, and that no backend_users row disappears; TestAdministratorsRevocation shows the target's earlier JWT answering 401 after deactivate, delete and a password set by another admin, and tokens_valid_after unchanged after restore and activate."
- "D-14, SC-3: TestActionExtensionBoot (-short) fails boot for an unknown controller, a target without a preview block, a duplicate of the owner's name, a duplicate between two contributors, a reserved name, a nil Run, an empty label and an unknown permission; TestActionExtensionRun offers a contributed action after the owner's only with the target's and the action's permissions and a true Applies, answers 409 when Applies turns false inside the transaction, and runs Run with TxFromContext and the locked record."
- "D-16, D-17, D-07: TestRolesLifecycle (system role code change 422, delete with holders including soft-deleted ones 403 naming the count, system role delete 403, bulk delete rollback) and TestRolesPermissionCeiling (a non-superuser holding backend.manage_roles cannot add or remove a code they lack) pass."
- "D-21: TestFieldTriggerBoot and TestPermissionRecordLocks (-short) and TestRolesCodeDeclaredLocks (Postgres) pass; the SPA tests in admin/tests/form (trigger.test.ts and the extended PermissionEditorField, formState, FormView and PreviewView tests) pass under npm --prefix admin run test, and admin/tests/browser/record-actions-wrap.test.ts passes under npm --prefix admin run test:browser."
- "Activation, D-04: TestBackendScreensActivation shows that plugins without admin controllers activate no admin (no secret read), that with controllers the two settings links, both permission codes and both controllers exist, and that developer and publisher roles hold neither code; examples/hello and the surf admin-prefix tests stay green."
- "D-07: TestRoleCeiling (-short) covers grantsCovered with exact codes, a holder wildcard covering exact codes, a role wildcard covered only by an equal or broader holder wildcard, and an empty role."
- "SC-1: the existing TestAdminCreateCommand, TestAdminResetPasswordCommand, account, auth and OpenAPI conformance tests pass unchanged, and scripts/check-phase19.6.1.sh refuses when any commit since the phase base that touches a phase file also touches modules/cabana/commands.go, token_commands.go or account.go."
- "D-18: scripts/check-phase19.6.1.sh --all exits 0, refusing a stage on any failed, skipped or zero-match test or a missing required name, and reporting NOT RUN for the Docker stage only when no Docker daemon answers (a failure when PHASE1961_REQUIRE_DOCKER=1)."
- { statement: "An SPA browser test renders the preview footer with three owner-style actions and two contributed actions with 40-character labels at 1024px width and asserts the footer wraps without any button overlapping the Edit button (UI-SPEC long-text row for contributed action labels).", verification: backstop }
- { statement: "An SPA unit test shows a radio permission editor with trigger {action: disable, field: is_superuser, condition: checked} read-only with the trigger note while the switch is on, editable when off, and its key absent from editablePayload while disabled.", verification: backstop }
- { statement: "An SPA unit test shows that with is_superuser missing from the schema the triggered editor is editable and its values are sent.", verification: backstop }
- { statement: "An SPA unit test shows that hiding every field of the active tab removes the tab, activates the first remaining tab, and shows a server error for the hidden field as a FormErrorBanner line.", verification: backstop }
- { statement: "An SPA unit test shows a checkbox editor with meta.permissionLocks rendering the locked code checked and disabled with the record note, and permissionValues omitting it from the payload.", verification: backstop }
artifacts:
- path: "modules/cabana/administrators_harness_test.go"
provides: "screens test env: seeded admins, roles, logins, calls with CSRF header, cleanup"
contains: "func newScreensEnv("
- path: "modules/cabana/administrators_lifecycle_postgres_test.go"
provides: "D-09 race harness and lifecycle tests"
contains: "TestAdministratorsLastSuperuser"
- path: "modules/cabana/action_extensions_test.go"
provides: "D-14 boot errors"
contains: "TestActionExtensionBoot"
- path: "admin/tests/form/trigger.test.ts"
provides: "trigger evaluation and FormView payload tests"
contains: "applyTriggers"
- path: "scripts/check-phase19.6.1.sh"
provides: "fail-closed phase gate"
contains: "REQUIRE_TESTS"
key_links:
- from: "modules/cabana/administrators_lifecycle_postgres_test.go"
to: "modules/cabana/export_test.go"
via: "the race holds the same advisory lock key the guard uses"
pattern: "SuperuserLockKey"
- from: "scripts/check-phase19.6.1.sh"
to: "modules/cabana/administrators_postgres_test.go"
via: "the docker stage requires every rule test by name"
pattern: "TestAdministratorsSelfEdit"
- from: "admin/tests/form/trigger.test.ts"
to: "admin/src/components/form/trigger.ts"
via: "unit tests of triggerState and applyTriggers"
pattern: "from '../../src/components/form/trigger'"
prohibitions:
- statement: "No production code changes in this plan except a fix a failing test proves necessary, which is recorded in the SUMMARY as a deviation with its own commit."
status: unresolved
verification: judgment
- statement: "Tests seed and clean up their own uniquely named admins and roles, never depend on another test's rows, and name no consuming application."
status: unresolved
verification: judgment
- statement: "A Docker-backed test that could not run is reported NOT RUN, never counted as passed."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not a user story; MVP mode off): cabana gets WinterCMS's Administrators screens with the privilege rules on the server; SC-3 asks that unit tests cover the privilege rules.
This plan's slice (plan 3 of 3, wave 3, D-18 and D-22: unit tests are the last plan): the tests behind every rule of D-05 to D-09 (with the concurrent last-superuser case), D-12 revocation, the D-14 seam, the Roles rules, D-19, D-20 and the D-21 SPA support, plus the phase gate script. The tracer builds the shared HTTP test env and the first rule test through it; the other tasks add the rule tests by area.
## Spec-less probe fallback
Skipped for edges (no requirement IDs, no SPEC.md; visible choice). The five UI-SPEC "UI Considerations" backstop rows are authored here as `{ statement, verification: backstop }` truths, because this plan writes those tests. Prohibitions recalled by hand, descriptor-less.
## Decisions taken in this plan (recorded assumptions and Claude's discretion)
- The last-superuser refusal is reachable only under concurrency through the screens (Plan 01 Decisions), so each path is proven with a race held on the advisory lock: a test transaction takes the guard's lock, both requests block on it after their row locks (visible as two ungranted advisory rows in pg_locks), the test releases, and the second request sees the first one's commit. `cabana.SuperuserLockKey` is exposed to tests through export_test.go only.
- D-09 tests isolate the superuser set: they record every other active superuser, deactivate them for the test and restore them in t.Cleanup (no cabana test uses t.Parallel); every test seeds uniquely named rows and soft-deletes and demotes them in t.Cleanup.
- Tests that need the compiled registry or unexported helpers live in package cabana (`-short`, no database); HTTP-level tests live in package cabana_test and use `adminGorm`, which skips under -short.
- The footer-wrap backstop needs real layout, so it runs in vitest browser mode (Playwright Chromium, admin/vitest.browser.config.ts), the same setup as admin/tests/browser/richeditor-display.test.ts.
- The gate script copies the fail-closed detectors of scripts/check-phase19.5.sh (`detect_go`, `detect_vitest`, `go_json`, `go_named`) and its consuming-application name check.
## Artifacts this phase produces (this plan)
- Test-only exports in modules/cabana/export_test.go: `SuperuserLockKey`, and a compile helper for the screens registry if the internal tests need it from cabana_test.
- Test env in modules/cabana/administrators_harness_test.go: `newScreensEnv(t)`, `screensEnv.seedAdmin(t, adminSeed)`, `screensEnv.seedRole(t, roleSeed)`, `screensEnv.login(t, login) string`, `screensEnv.call(t, method, rel, body, token)`, `screensEnv.isolateSuperusers(t, keep ...uint)`, the contributor test plugin with `acme_ping`.
- Go tests: TestAdministratorsSuperuserFlag, TestAdministratorsSuperuserHidden, TestAdministratorsSuperuserProjected, TestAdministratorsPermissionCeiling, TestAdministratorsRoleCeiling, TestAdministratorsSelfEdit, TestAdministratorsPasswordAndIdentity, TestAdministratorsOutranked, TestAdministratorsOwnPassword, TestAdministratorsLastSuperuser, TestAdministratorsLastSuperuserConcurrent, TestAdministratorsLifecycle, TestAdministratorsRevocation, TestActionExtensionBoot, TestActionExtensionRun, TestRolesLifecycle, TestRolesPermissionCeiling, TestRolesCodeDeclaredLocks, TestRoleCeiling, TestBackendScreensActivation, TestFieldTriggerBoot, TestPermissionRecordLocks.
- SPA tests: admin/tests/form/trigger.test.ts, additions to PermissionEditorField.test.ts, formState.test.ts, FormView.test.ts, PreviewView.test.ts, and admin/tests/browser/record-actions-wrap.test.ts.
- `scripts/check-phase19.6.1.sh` with stages go, docker, admin, docs, sc1 and `--all`.
- 19.6.1-VALIDATION.md per-task map statuses updated, Wave 0 ticked, `wave_0_complete: true`.
<objective>
Write the unit and Postgres tests for every privilege rule and seam of the phase, the SPA tests for D-21 and the phase gate script, last per the project rule (D-18, D-22 plan 03).
Purpose: SC-2 and SC-3 (unit tests cover the privilege rules), SC-1 regression proof.
Output: test files, the gate script, the filled validation map; test commits separate from any production fix.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-CONTEXT.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-RESEARCH.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-UI-SPEC.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-01-SUMMARY.md
@.planning/phases/19.6.1-backend-administrators-screen/19.6.1-02-SUMMARY.md
Concurrency and commits: as in Plans 01 and 02. Docker-backed tests skip under -short; run them on this host, through `sudo -u golem` if the Docker socket is denied (CLAUDE.md); a test that could not run is NOT RUN in the SUMMARY. A test that fails because the code is wrong gets a production fix in its own `fix(cabana): ...` commit, named in the SUMMARY as a deviation; never weaken an assertion to pass.
<interfaces>
- From Plans 01 and 02 (their SUMMARYs list the names as built): `cabana.PermissionManageUsers`, `PermissionManageRoles`, `AdministratorsControllerID`, `RolesControllerID`; pact `AdminActionExtension`, `HasAdminActionExtensions`; unexported `withBackendScreens`, `compileActionExtensions`, `grantsCovered`, `guardLastSuperuser`, `superuserLockKey`, `compileTrigger`, `checkTriggers`, `applyPermissionValues`; `cabana.PermissionRecordLock`, `PermissionRecordLockProvider`, `RecordMeta.PermissionLocks`, `FieldTrigger`, `FormField.Trigger`; smoke tests in backend_screens_test.go, backend_screens_smoke_test.go, record_locks_smoke_test.go, field_trigger_smoke_test.go; admin routes `/summercms/cabana/users` and `/summercms/cabana/roles` (list, `{id}`, `{id}/actions/{action}`, `bulk/{action}`, `bulk-delete`, `schema/form`, `schema/list`), `/settings`, `/auth/me`, `/auth/login`.
- Test helpers (package cabana_test): `adminGorm(t)` auth_test.go:321, `insertAdmin` :403, `demoPlugin`, `postJSON`, `adminAPI`, `accessToken`, `adminTestPassword`, `adminTestSecret`, `adminSQL`; env and call pattern phase101_actions_test.go:225-346 (`X-Requested-With: XMLHttpRequest` on writes, `cabana.Envelope[T]`); internal fixtures (package cabana) `contributionPlugin`, `metadataRegistry` settings_link_test.go; internal service handlers called with `bouncer.WithUser` (settings_link_test.go:150-200); export_test.go pattern (`CompiledListSchema`).
- Race tools: `pg_locks` (locktype 'advisory', granted false), `SELECT pg_advisory_xact_lock(?)` in a `gdb.Begin()` transaction, released by Rollback.
- SPA tests: admin/tests/form/PermissionEditorField.test.ts, formState.test.ts, FormView.test.ts, PreviewView.test.ts, RecordActions.test.ts, DependentDropdown.test.ts (sibling evaluation), admin/tests/browser/richeditor-display.test.ts and admin/vitest.browser.config.ts, fixtures under admin/tests/fixtures/.
- Gate analog: scripts/check-phase19.5.sh (`detect_go` :189, `detect_vitest` :239, `go_json` :261, `go_named` :288, `run_docs` :721 with APP_NAMES).
</interfaces>
</context>
<tasks>
<task type="tracer">
<name>Task 1: Tracer: the screens test env seeds admins and roles, signs in, and proves the first privilege rule (D-05) end to end through the assembled admin handler</name>
<reversibility rating="reversible">Test-only code.</reversibility>
<files>modules/cabana/export_test.go, modules/cabana/administrators_harness_test.go, modules/cabana/administrators_postgres_test.go</files>
<read_first>.planning/phases/19.6.1-backend-administrators-screen/19.6.1-01-SUMMARY.md, .planning/phases/19.6.1-backend-administrators-screen/19.6.1-02-SUMMARY.md, modules/cabana/backend_screens_smoke_test.go, modules/cabana/auth_test.go (:300-440), modules/cabana/phase101_actions_test.go (:225-346), modules/cabana/export_test.go, modules/cabana/backend_users_admin.go, modules/cabana/backend_privileges.go, 19.6.1-UI-SPEC.md (C8), 19.6.1-RESEARCH.md (Validation Architecture)</read_first>
<action>Per D-18 and D-05.
export_test.go: `const SuperuserLockKey = superuserLockKey` with a comment (the race tests hold the guard's lock); add a compile helper only if a cabana_test test needs the screens registry.
administrators_harness_test.go (package cabana_test): `newScreensEnv(t)` assembles like newActEnv (compass config with the admin secret and body limits, lagoon.Publish, phrasebook.Activate, surf.Assemble) with demoPlugin plus a contributor plugin (its own permission code acme.screens.ping and a role grant of that code to nothing) that adds record action acme_ping to AdministratorsControllerID and records each Run's RecordID and whether cabana.TxFromContext held a transaction. `seedAdmin(t, adminSeed{Superuser, Activated, Deleted, RoleCode, RolePermissions map, Permissions map, Password})` inserts with a unique login and email from a nanosecond stamp (a seeded role per call when RolePermissions is set, else the developer role), and registers t.Cleanup that sets is_superuser false and soft-deletes the row through SQL. `seedRole(t, roleSeed{Name, Code, Permissions, System})` likewise. `login(t, login) string` posts /auth/login and returns the bearer token. `call(t, method, rel, body, token)` sends JSON with Accept-Language en and X-Requested-With on writes. `isolateSuperusers(t, keep ...uint)` deactivates every other active superuser and restores them in t.Cleanup. Small decoders for the data envelope and the error envelope (code, message, details).
administrators_postgres_test.go: `TestAdministratorsSuperuserFlag` (D-05): a superuser S and a delegated admin L (role grants backend.manage_users); L creating an admin with is_superuser true answers 403, message and details.is_superuser carry the en superuser_forbidden text, and no row with that login exists; L updating a non-superuser N with is_superuser false answers 403 and N's row is byte-for-byte unchanged (compare updated_at and the columns); S creates an admin with is_superuser true (201, row flag true) and then removes it from another superuser through update (200, flag false).</action>
<verify>
<automated>go vet ./modules/cabana &amp;&amp; go test -count=1 -short ./modules/cabana &amp;&amp; go test -count=1 -run '^TestAdministratorsSuperuserFlag$' -v ./modules/cabana 2&gt;&amp;1 | grep -q -- '^--- PASS: TestAdministratorsSuperuserFlag'</automated>
<fails_when>Non-zero exit from vet or the short suite, or no "--- PASS: TestAdministratorsSuperuserFlag" line (failed, or "--- SKIP" because Docker was unreachable: rerun through sudo -u golem, else NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'SuperuserLockKey' modules/cabana/export_test.go` prints at least 1.
- `grep -c 'func newScreensEnv(' modules/cabana/administrators_harness_test.go` prints 1 and `grep -cE 'func \([a-z]+ \*screensEnv\) isolateSuperusers\(' modules/cabana/administrators_harness_test.go` prints 1.
- `grep -c 't.Cleanup(' modules/cabana/administrators_harness_test.go` prints at least 2.
- `grep -ciE 'fonoteka|plytarium|grzyby|wavepath' modules/cabana/administrators_harness_test.go modules/cabana/administrators_postgres_test.go` prints 0 for each file.
</acceptance_criteria>
<done>One rule is proven over real HTTP against Postgres, and every later test reuses the same env.</done>
</task>
<task type="auto" tdd="true">
<name>Task 2: Save-path rule tests: superuser hiding and projection, permission and role ceilings, self-edit, passwords and identity, outranking and own password</name>
<reversibility rating="reversible">Test-only code.</reversibility>
<files>modules/cabana/administrators_postgres_test.go, modules/cabana/backend_privileges_internal_test.go</files>
<read_first>modules/cabana/administrators_harness_test.go, modules/cabana/administrators_postgres_test.go (from Task 1), modules/cabana/backend_users_admin.go, modules/cabana/backend_privileges.go, modules/cabana/contracts.go (granted :214-248), modules/cabana/field_permission.go, modules/cabana/relation_field.go (RelationLock), 19.6.1-UI-SPEC.md (C3, C8), 19.6.1-RESEARCH.md (Pattern 5, Pitfalls 1, 2, 4, 9)</read_first>
<behavior>
- TestAdministratorsSuperuserHidden (D-03): for delegated admin L the list (also with search on the superuser's login and with filters) never contains superuser S; GET, the hint partial and a record action on S's id answer 404; schema/form has no is_superuser field and schema/list no is_superuser column or filter; for S all three exist.
- TestAdministratorsSuperuserProjected (Pitfall 1): S's GET of another superuser returns data.is_superuser true; PUT of exactly the editable payload of that record (as the SPA builds it) answers 200 and the flag stays true.
- TestAdministratorsPermissionCeiling (D-06, D-07): L (holding backend.manage_users and acme.screens.ping only) sees every other catalog code Locked in schema/form; setting a locked code to 1 or -1 on N answers 403 on permissions and N's stored JSON is unchanged; a stored locked value N already has is kept when L saves other fields; allowing or denying acme.screens.ping on N saves and FindByID-equivalent GET /auth/me as N reflects it (the override merged last).
- TestAdministratorsRoleCeiling (D-07): roles whose grants L does not cover are locked in the role field options; assigning one to N answers 403 with the role_ceiling text; N holding an above-ceiling role saves a name change unchanged in role; assigning a covered role saves.
- TestAdministratorsSelfEdit (D-08): L edits own first name, login and email (200); own role change answers 403 self_privileged and the role is unchanged (the after-assignment path); own is_superuser for S answers 403; own permissions change answers 403; own delete answers 403 self_delete; own bulk deactivate answers 403 self_deactivate; the record action deactivate is not offered on your own preview.
- TestAdministratorsPasswordAndIdentity (D-11): create without password, without confirmation or with a mismatch answers 422 on password; 3 characters answers 422; 73 bytes answers 422 with the max text; a valid create stores a bcrypt hash that bouncer verifies; update with a blank password keeps the hash; a login or email equal to another admin's, in another case, or held by a soft-deleted admin answers 422 on that field with login_taken, email_taken or the _deleted variants.
- TestAdministratorsOutranked (D-19): L acting on M whose role grants a code L lacks gets 403 outranked on update (also a password set), delete, record activate, deactivate and restore, and bulk actions, and M's row is unchanged; acting on N with covered grants passes.
- TestAdministratorsOwnPassword (D-20): L or S sending a password for their own record answers 403 own_password on password and the hash is unchanged; another admin's password set by S answers 200.
- TestRoleCeiling (package cabana, -short): grantsCovered with exact codes, a holder wildcard covering exact codes, a role wildcard covered by the same or a broader holder wildcard and not by exact codes, a suffix wildcard, an empty role, a nil holder.
</behavior>
<action>Per D-03, D-05..D-08, D-11, D-19, D-20 and RESEARCH Pitfalls 1, 2, 4, 9. Write the behaviours above as table-driven Go tests where the cases share setup, every refusal asserting the HTTP status, the error code, the en text of the phrase key (read from the translator or compared to the UI-SPEC English value) in message or details as UI-SPEC C8 places it, and the unchanged row (re-read the columns). Seed through the env helpers, one set of rows per test. backend_privileges_internal_test.go holds TestRoleCeiling against grantsCovered directly. These tests describe Plan 01's built behaviour; when one fails, fix the code in a separate fix commit and record it.</action>
<verify>
<automated>go vet ./modules/cabana &amp;&amp; go test -count=1 -short -run '^TestRoleCeiling$' -v ./modules/cabana 2&gt;&amp;1 | grep -q -- '^--- PASS: TestRoleCeiling'</automated>
<fails_when>Non-zero exit from vet or no "--- PASS: TestRoleCeiling" line.</fails_when>
<automated>go test -count=1 -run '^(TestAdministratorsSuperuserFlag|TestAdministratorsSuperuserHidden|TestAdministratorsSuperuserProjected|TestAdministratorsPermissionCeiling|TestAdministratorsRoleCeiling|TestAdministratorsSelfEdit|TestAdministratorsPasswordAndIdentity|TestAdministratorsOutranked|TestAdministratorsOwnPassword)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 9</automated>
<fails_when>Fewer than 9 top-level "--- PASS: Test" lines: a rule test failed, was renamed, or skipped because Docker was unreachable (rerun through sudo -u golem, else NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -cE '^func Test(AdministratorsSuperuserHidden|AdministratorsSuperuserProjected|AdministratorsPermissionCeiling|AdministratorsRoleCeiling|AdministratorsSelfEdit|AdministratorsPasswordAndIdentity|AdministratorsOutranked|AdministratorsOwnPassword)\(' modules/cabana/administrators_postgres_test.go` prints 8.
- `grep -c '^func TestRoleCeiling(' modules/cabana/backend_privileges_internal_test.go` prints 1 and `head -1 modules/cabana/backend_privileges_internal_test.go` prints `package cabana`.
- `grep -c 'own_password\|Change your own password' modules/cabana/administrators_postgres_test.go` prints at least 1.
</acceptance_criteria>
<done>Every save-path rule has a test that fails when the rule is removed.</done>
</task>
<task type="auto" tdd="true">
<name>Task 3: Lifecycle, last-superuser race, revocation, seam and Roles tests</name>
<reversibility rating="reversible">Test-only code.</reversibility>
<files>modules/cabana/administrators_lifecycle_postgres_test.go, modules/cabana/action_extensions_test.go, modules/cabana/action_extensions_postgres_test.go, modules/cabana/roles_postgres_test.go, modules/cabana/backend_screens_test.go</files>
<read_first>modules/cabana/administrators_harness_test.go, modules/cabana/backend_screens_test.go, modules/cabana/action_extensions.go, modules/cabana/backend_users_admin.go, modules/cabana/backend_roles_admin.go, modules/cabana/backend_privileges.go, modules/cabana/crud.go (:340-380, :586-640), modules/cabana/settings_link_test.go, modules/cabana/phase121_schema_boot_test.go, modules/cabana/example_actions_test.go, modules/surf/admin_prefix_test.go (:180-200), 19.6.1-RESEARCH.md (Pattern 2, Pattern 4, Pitfalls 5, 7)</read_first>
<behavior>
- TestAdministratorsLastSuperuser (D-09), subtests demote_update, deactivate_record, deactivate_bulk, delete_form, delete_bulk: isolateSuperusers keeping fresh A and B; a test tx holds pg_advisory_xact_lock(cabana.SuperuserLockKey); A acts on B and B acts on A concurrently; poll pg_locks until two ungranted advisory rows exist (fail after 10 s); roll the test tx back; exactly one 2xx and one 403 with the last_superuser text; exactly one of A and B is an active, non-deleted superuser afterwards; no 500 and no "deadlock" in any body.
- TestAdministratorsLastSuperuserConcurrent (D-09): ten rounds of unsynchronized A demotes B and B demotes A; each round ends with at least one active superuser and no 500.
- TestAdministratorsLifecycle (D-10): record and bulk activate, deactivate and restore with Applies (activate offered only when inactive and not deleted, restore only when deleted, deactivate not on yourself); bulk skips rows that do not apply and reports Affected; soft delete keeps the row (count of backend_users unchanged) with row state deleted; the trashed filter options hide and only narrow the list; the preview hint follows deleted > deactivated > own account.
- TestAdministratorsRevocation (D-12): the target's JWT issued before the action answers 401 on /auth/me after deactivate (record and bulk), after delete, and after a password set by S; the target's personal API token stops after deactivate and delete; tokens_valid_after is unchanged after restore and activate.
- TestActionExtensionBoot (package cabana, -short): compile errors for an unknown controller, a target form without preview, a duplicate of the owner's record action, a duplicate between two contributors (message names both plugins), create or delete as a name, a nil Run, an empty label, a permission no plugin registers; a valid contribution lands after the owner's names in declared order across two contributors.
- TestActionExtensionRun (Postgres): acme_ping is offered on an admin's preview only to principals holding both backend.manage_users and acme.screens.ping; a principal with only acme.screens.ping gets 403 from the route; with Applies flipped to false between show and run the POST answers 409; Run saw the RecordID and a transaction.
- TestRolesLifecycle (D-16, D-17): create with a slugged code; duplicate code answers 422 code_taken; changing developer's code answers 422 code_locked; a role a plugin's code-declared grant attaches to counts as system; delete with one active and one soft-deleted holder answers 403 whose message names the role and 2; delete of developer answers 403 delete_system; bulk delete of an unused role plus a held one answers 403 and both rows remain.
- TestRolesPermissionCeiling (D-07): a principal holding backend.manage_roles and acme.screens.ping sees other codes Locked on the Roles form, adding or removing a locked code answers 403 and the role JSON is unchanged, toggling acme.screens.ping saves.
- TestBackendScreensActivation (package cabana, -short): no admin controllers → Activate returns nil routes and nil error without admin.jwt.secret set; with a controller plugin → the registry holds both controllers, both settings links (administrators before roles), both permission codes with no role grants, and no navigation entry from summercms.cabana.
</behavior>
<action>Per D-04, D-07, D-09, D-10, D-12, D-14, D-15, D-16, D-17 and SC-3. Write the behaviours above. The race helper lives in administrators_lifecycle_postgres_test.go and takes a func(actor, target) performing one path, so the five subtests share it; wait on pg_locks with `SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND NOT granted` every 20 ms. Personal API tokens for the revocation test are minted the way token_postgres_smoke_test.go does. Boot tests reuse the internal fixtures and assert error substrings (controller ID, action name, plugin IDs). When a test fails because the code is wrong, fix the code in a separate fix commit and record it.</action>
<verify>
<automated>go vet ./modules/cabana &amp;&amp; go test -count=1 -short -run '^(TestActionExtensionBoot|TestBackendScreensActivation|TestBackendScreensSmoke)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 3</automated>
<fails_when>Non-zero exit from vet, or fewer than 3 top-level "--- PASS: Test" lines among the Docker-free seam and activation tests.</fails_when>
<automated>go test -count=1 -run '^(TestAdministratorsLastSuperuser|TestAdministratorsLastSuperuserConcurrent|TestAdministratorsLifecycle|TestAdministratorsRevocation|TestActionExtensionRun|TestRolesLifecycle|TestRolesPermissionCeiling)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 7</automated>
<fails_when>Fewer than 7 top-level "--- PASS: Test" lines: a test failed (including the race timing out waiting for two lock waiters), was renamed, or skipped because Docker was unreachable (rerun through sudo -u golem, else NOT RUN).</fails_when>
</verify>
<acceptance_criteria>
- `grep -c 'SuperuserLockKey' modules/cabana/administrators_lifecycle_postgres_test.go` prints at least 1 and `grep -c "locktype = 'advisory'" modules/cabana/administrators_lifecycle_postgres_test.go` prints at least 1.
- `grep -cE 't\.Run\("(demote_update|deactivate_record|deactivate_bulk|delete_form|delete_bulk)"' modules/cabana/administrators_lifecycle_postgres_test.go` prints 5.
- `grep -c '^func TestActionExtensionBoot(' modules/cabana/action_extensions_test.go` prints 1 and `head -1 modules/cabana/action_extensions_test.go` prints `package cabana`.
- `grep -cE '^func Test(RolesLifecycle|RolesPermissionCeiling)\(' modules/cabana/roles_postgres_test.go` prints 2.
- `grep -c 't.Parallel()' modules/cabana/administrators_lifecycle_postgres_test.go` prints 0.
</acceptance_criteria>
<done>The concurrent last-superuser case, every lifecycle path, revocation, the seam and the Roles rules are proven.</done>
</task>
<task type="auto" tdd="true">
<name>Task 4: D-21 tests (Go and SPA, including the backstops), the phase gate script and the validation map</name>
<reversibility rating="reversible">Tests, a check script and planning metadata.</reversibility>
<files>modules/cabana/field_trigger_test.go, modules/cabana/record_locks_test.go, modules/cabana/roles_postgres_test.go, admin/tests/form/trigger.test.ts, admin/tests/form/PermissionEditorField.test.ts, admin/tests/form/formState.test.ts, admin/tests/form/FormView.test.ts, admin/tests/form/PreviewView.test.ts, admin/tests/browser/record-actions-wrap.test.ts, scripts/check-phase19.6.1.sh, .planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md</files>
<read_first>.planning/phases/19.6.1-backend-administrators-screen/19.6.1-02-SUMMARY.md, modules/cabana/field_trigger.go, modules/cabana/field_trigger_smoke_test.go, modules/cabana/record_locks_smoke_test.go, modules/cabana/field_permission.go, admin/src/components/form/trigger.ts, admin/src/components/form/fields/PermissionEditorField.vue, admin/src/components/form/formState.ts, admin/src/components/form/control.ts, admin/src/views/FormView.vue, admin/src/views/PreviewView.vue, admin/src/components/form/RecordActions.vue, admin/tests/form/PermissionEditorField.test.ts, admin/tests/form/formState.test.ts, admin/tests/form/FormView.test.ts, admin/tests/form/PreviewView.test.ts, admin/tests/form/RecordActions.test.ts, admin/tests/browser/richeditor-display.test.ts, admin/vitest.browser.config.ts, scripts/check-phase19.5.sh, .planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md, 19.6.1-UI-SPEC.md (C6, C7, UI Considerations backstop rows)</read_first>
<behavior>
- TestFieldTriggerBoot (package cabana, -short): every refusal rule of compileTrigger and checkTriggers (missing or extra sub-key, unknown action or condition, unknown, self or non-toggle controlling field, trigger in a repeater row, a settings form and a relation form), each error naming field and key; valid disable and hide triggers reach the localized schema.
- TestPermissionRecordLocks (package cabana, -short): the save rule (absent keeps stored, equal passes, different answers 403 on the field, request-locked plus record-locked absent passes) for radio and checkbox modes; meta omits permissionLocks without a provider and on create.
- TestRolesCodeDeclaredLocks (Postgres): with a test plugin declaring acme.screens.report for the developer role, GET of developer carries meta.permissionLocks.permissions with acme.screens.report value 1 and the en locked_by_code note; saving developer's description with the SPA payload (which omits the code) answers 200; the delegated admin's own record carries every offered code record-locked with the en locked_self note.
- trigger.test.ts: triggerState for checked and unchecked against true, 1, "1", "true", false and missing values; a controlling field absent from present counts as off; applyTriggers disable makes the field readOnly with the note, hide removes it.
- FormView.test.ts additions: disable backstop (switch on → editor read-only with the data-trigger-note line, key absent from the save body; switch off → editable and sent); absent-controller backstop (schema without is_superuser → editable and sent); hide backstop (all fields of the active tab hidden → tab removed, first tab active, a 422 detail for the hidden field shown in FormErrorBanner); announce text on disable.
- PermissionEditorField.test.ts and formState.test.ts additions: record-lock backstop (checkbox editor with permissionLocks shows the code checked and disabled with data-locked-by="record" and the record note; permissionValues and editablePayload omit it); a radio lock shows the lock value; record lock wins over request lock; read-only screen shows no icon or note.
- PreviewView.test.ts addition: hide trigger removes the field on the preview; disable shows no note.
- record-actions-wrap.test.ts (browser): the footer backstop at 1024px with three owner actions and two contributed actions labelled with 40 characters: the buttons wrap onto more than one row and no button's bounding box intersects the Edit button's.
</behavior>
<action>Per D-21, D-18 and UI-SPEC C6, C7 and the backstop rows. Write the behaviours above (Go tests table-driven; SPA tests with @vue/test-utils and fixtures shaped like admin/tests/fixtures/*.form-schema.json; the browser test mounts RecordActions inside the preview footer markup at a 1024px viewport).
scripts/check-phase19.6.1.sh (executable, `set -euo pipefail`, header comment listing stages like check-phase19.5.sh): copy detect_go, detect_vitest, go_json and go_named from scripts/check-phase19.5.sh. Stages: `go` (go vet ./..., go test -short ./..., go -C examples/hello test ./..., and go_named with -short for TestBackendScreensSmoke, TestBackendScreensActivation, TestActionExtensionBoot, TestRoleCeiling, TestFieldTriggerBoot, TestFieldTriggerSmoke, TestPermissionRecordLocks, TestPermissionRecordLocksSmoke); `docker` (go_named without -short for every Postgres test of Plans 01 and 03 by name, plus go test ./modules/surf; NOT RUN when `docker info` fails, a failure when PHASE1961_REQUIRE_DOCKER=1); `admin` (npm --prefix admin run typecheck, the vitest run through detect_vitest requiring tests/form/trigger.test.ts, PermissionEditorField.test.ts, formState.test.ts, FormView.test.ts and PreviewView.test.ts, npm --prefix admin run test:browser requiring record-actions-wrap.test.ts, scripts/check-admin-openapi.sh --check, scripts/check-admin-dist.sh); `docs` (go_named TestDocsTree in ./cmd/summer, go run ./cmd/summer docs:build --check, and the consuming-application name check of check-phase19.5.sh over modules/ and docs/); `sc1` (PHASE1961_BASE, default the parent of the first commit that added modules/cabana/backend_screens.go: refuse when a commit in BASE..HEAD touches modules/cabana/commands.go, token_commands.go or account.go and also a phase file: action_extensions.go, backend_screens.go, backend_users_admin.go, backend_roles_admin.go, backend_privileges.go, field_trigger.go); `--all` runs go, admin, docs, sc1, then docker. Each stage prints one pass line; any refusal exits non-zero.
19.6.1-VALIDATION.md: the planner already filled the per-task verification map; update its File Exists and Status columns from the runs, correct any test or file name that changed during execution, tick the Wave 0 list with the file names as built, and set `wave_0_complete: true`; leave `status` and `nyquist_compliant` for validate-phase. Commit tests (`test(cabana): ...`, `test(admin): ...`), the script (`chore(scripts): check-phase19.6.1 gate`) and the planning doc (`docs(19.6.1): validation map`) separately.</action>
<verify>
<automated>go test -count=1 -short -run '^(TestFieldTriggerBoot|TestPermissionRecordLocks)$' -v ./modules/cabana 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 2 &amp;&amp; npm --prefix admin run test &amp;&amp; npm --prefix admin run test:browser</automated>
<fails_when>Fewer than 2 top-level "--- PASS: Test" lines, or a non-zero exit from the vitest run or the browser run (a failed, missing or skipped SPA test).</fails_when>
<automated>bash scripts/check-phase19.6.1.sh --all</automated>
<fails_when>Non-zero exit or a line starting "refuse:"; a "NOT RUN docker" line is acceptable only when no Docker daemon answers and must be recorded in the SUMMARY.</fails_when>
</verify>
<acceptance_criteria>
- `test -x scripts/check-phase19.6.1.sh` exits 0 and `grep -c 'REQUIRE_TESTS' scripts/check-phase19.6.1.sh` prints at least 1.
- `grep -c 'TestAdministratorsLastSuperuserConcurrent' scripts/check-phase19.6.1.sh` prints at least 1 and `grep -c 'PHASE1961_REQUIRE_DOCKER' scripts/check-phase19.6.1.sh` prints at least 1.
- `grep -c "from '../../src/components/form/trigger'" admin/tests/form/trigger.test.ts` prints 1.
- `grep -c 'data-locked-by' admin/tests/form/PermissionEditorField.test.ts` prints at least 1 and `grep -c '1024' admin/tests/browser/record-actions-wrap.test.ts` prints at least 1.
- `grep -c '^wave_0_complete: true' .planning/phases/19.6.1-backend-administrators-screen/19.6.1-VALIDATION.md` prints 1.
</acceptance_criteria>
<done>D-21 and every backstop have tests, and one script proves the whole phase fail-closed.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| test suite → privilege rules | A rule without a test can regress silently |
| shared Postgres container → tests | Rows from one test can change another test's outcome |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.6.1-24 | Repudiation | privilege regressions (D-05..D-09, D-19, D-20) going unnoticed | high | mitigate | one named test per rule asserting status, text and unchanged rows; the gate requires every name and refuses skipped or zero-match runs |
| T-19.6.1-25 | Denial of service | a deadlock in the last-superuser guard hidden by a flaky race | high | mitigate | the held-lock race waits for two pg_locks waiters before releasing, so the interleaving is forced on every path; the unsynchronized rounds assert no 500 or deadlock |
| T-19.6.1-26 | Tampering | cross-test interference through shared superuser rows | medium | mitigate | isolateSuperusers plus per-test unique rows and cleanup; no t.Parallel in the package |
| T-19.6.1-27 | Information disclosure | tests or fixtures naming consuming applications | low | mitigate | the gate's docs stage name check and the acceptance greps |
| T-19.6.1-SC | Tampering | npm/pip/cargo installs | high | mitigate | no package is added; vitest browser mode uses the Playwright already in the lockfile; any new package would need the legitimacy gate and a blocking human checkpoint |
</threat_model>
<verification>
- `go test -short ./modules/cabana` and the named Docker-free tests after every task; the named Postgres tests per task with Docker.
- `npm --prefix admin run test` and `test:browser` after Task 4.
- `bash scripts/check-phase19.6.1.sh --all` as the phase gate (also the input for verify-work).
</verification>
<success_criteria>
- Every decision with a server rule (D-03..D-12, D-14, D-16, D-17, D-19, D-20, D-21) has at least one named test that fails without the rule.
- The concurrent last-superuser case is proven on all five reducing paths.
- The gate script passes on this host with Docker, and reports NOT RUN honestly without it.
</success_criteria>
<output>
Create `.planning/phases/19.6.1-backend-administrators-screen/19.6.1-03-SUMMARY.md` when done
</output>

View File

@@ -0,0 +1,188 @@
# Phase 19.6.1: Backend Administrators screen - Context
**Gathered:** 2026-10-21
**Status:** Ready for planning
<domain>
## Phase Boundary
cabana ships its own **Administrators** and **Roles** admin screens, in the style of WinterCMS `Backend\Controllers\Users` and `UserRoles`. Backend admins and roles can then be managed in the admin SPA, and not only through the `admin:*` console commands.
The screens:
- are built on cabana's existing generic CRUD (list, form and preview renderer), the `permissioneditor` field and record actions;
- enforce the privilege rules on the server;
- expose a generic seam through which plugins add record actions to a controller they don't own. 19.7's frontend-link action is the first consumer (19.7 D-34).
The phase is kept short (1-2 plans). Existing cabana payloads and the `admin:*` commands stay unchanged. Only `summercms.go` is touched.
</domain>
<decisions>
## Implementation Decisions
### Permissions and placement
- **D-01:** The screens are guarded by **two new permission codes**:
- `backend.manage_users` for Administrators (Winter's name is kept);
- `backend.manage_roles` for Roles.
Roles change what every holder can do, so they get their own code. These are the first permissions cabana itself registers, through an internal pseudo-plugin (see Code Insights). — **Reversibility:** costly — the permission codes end up stored in `backend_user_roles.permissions` and `backend_users.permissions` JSON on every install that grants them.
- **D-02:** **Placement: Settings → System section**, as in Winter, through `pact.HasSettings` entries that link into the controllers. No top-level navigation item.
- **D-03:** **Superusers are hidden from non-superusers**, as in Winter:
- `ListExtendQuery` and `FormExtendQuery` filter `is_superuser` rows out;
- the `is_superuser` field and filter are removed for non-superusers;
- opening a superuser record by ID answers 404 or 403.
- **D-04:** **No default role grant.** Neither code is added to the `developer` or `publisher` system roles through code-declared `Roles`. Only superusers reach the screens until someone grants the codes, so existing installs don't widen silently on upgrade.
### Privilege rules (server-side, all enforced in the save transaction)
- **D-05:** **Only a superuser can create or edit a superuser, or set `is_superuser`** (roadmap SC-2). A non-superuser sending `is_superuser` is answered 403, even if the field is hidden.
- **D-06:** **The per-user permission override is editable**: a permissions tab on the Administrators form with `permissioneditor` mode **radio** (inherit / allow / deny), as in Winter. It is disabled when `is_superuser` is on. The column and its merge (`applyUserPermissions`, merged last) already exist.
- **D-07:** **A non-superuser can't grant more than they hold**, and this covers both permissions and role assignment.
- **Permission editors**, on both the per-user tab and the Roles screen: an option the editor doesn't hold is `PermissionOption.Locked`, and its stored value is kept on save (Winter's `getSaveValueSecure`). Changing a locked value is answered 403, the existing behaviour.
- **Role picker**: it offers only roles whose merged grants (stored JSON plus code-declared grants) are a subset of the editor's own permissions. Assigning any other role is refused on the server.
- **Roles screen**: a non-superuser can't add a permission they lack to any role.
- **D-08:** **Editing your own record is allowed, but privileged fields are read-only.**
- The admin can edit their profile fields on the Administrators screen.
- The server refuses, for the editor's own record, any change to `role_id`, `is_superuser`, `permissions` or `is_activated`, and refuses deleting yourself.
- This covers "an admin cannot raise their own role or permissions" (SC-2) in one place. There is no redirect to My Account.
- **D-09:** **The last active superuser is protected on every write path.** It can't be deactivated, demoted (`is_superuser` off), soft-deleted, or turned inactive by any route. The check covers:
- form update and delete;
- bulk actions, including bulk deactivate and bulk delete;
- record actions;
- any `admin:*` command path that could clear the flag.
It runs inside the save transaction under a lock (for example a row lock on the active superuser set, or an advisory lock), so two concurrent demotions can't both pass. Today's `admin:*` commands don't demote, so their behaviour and output stay unchanged (SC-1).
### Lifecycle and passwords
- **D-10:** **Deactivate + soft delete + restore.**
- Activate and Deactivate are record and bulk actions that toggle `is_activated`.
- Delete is a soft delete through the existing `deleted_at`.
- A trashed filter lists deleted admins, and a Restore action brings them back.
- No hard delete, so FK references (tokens, `linked_by`, attachments) stay valid.
- **D-11:** **Passwords are typed in the form.** Password and confirmation fields are required on create and optional on update, and blank means unchanged. The value is bcrypt-hashed like `admin:create`. There is no "send reset link" action and no force-change-on-next-login in this phase.
- **D-12:** **Changes cut access immediately.** Deactivate, soft delete, and setting a password for another admin all bump `tokens_valid_after`, as `admin:reset-password` does. Existing JWTs then stop on the next request. Deactivate and soft delete also stop personal API tokens, because `FindByID` already refuses inactive or deleted users. A password set for another admin leaves their personal API tokens working, the same as `admin:reset-password` (confirmed 2026-10-22 at plan check). Restore and activate don't bump it.
- **D-13:** **No invite or welcome mail** on create in this phase. Winter's `send_invite` is deferred.
### Record-action seam
- **D-14:** **A generic cross-controller action extension.** It is a new optional `pact` capability through which a plugin contributes `AdminRecordAction`s to any admin controller by controller ID, including cabana's Administrators controller and other plugins' controllers.
- It may also cover toolbar and bulk actions if the shape allows it at no extra cost (planner's call).
- Contributions merge at boot. A duplicate action name on a controller, or an unknown controller ID, is a boot error.
- Each action keeps its own `Permissions`, `Applies` and `Confirm`. The existing row-locked transactional run path (`crud.go` `RecordAction`) runs it.
- The seam is additive and names neither MCP nor the user plugin. It is the seam 19.7 D-04/D-34 consumes. The researcher checks whether 19.6 D-15 ("Reset 2FA" contributed by sm-mfa-plugin to the user plugin's Users screen) can use it as is.
— **Reversibility:** costly — this is a published `pact` capability that plugins (19.7's link action, sm-mfa-plugin) implement.
- **D-15:** **Contributed actions appear on the preview screen only**, through the existing `RecordActions.vue` on `PreviewView`. The Administrators controller therefore gets a preview block. There are no per-row actions on the list and no list payload changes.
### Roles screen
- **D-16:** **Fields and system roles follow Winter.**
- Fields: name, code (slugged from name), description, and a permissions tab using `permissioneditor` mode **checkbox**.
- System roles (`is_system`, or roles that code-declared `pact.Permission.Roles` grants attach to) have their code locked, and their code-declared grants are shown as locked.
- Name, description and any extra stored grants stay editable, within the D-07 ceiling.
- **D-17:** **A role that still has admins can't be deleted.** The delete returns a validation error that names how many admins hold the role. System roles can't be deleted.
### Quality gates
- **D-18:** Unit tests are the last plan (project rule). They cover every privilege rule in D-05 to D-09, including the concurrent last-superuser case, plus D-12 revocation and the D-14 seam (boot errors, permission filtering, run path). Docs: the cabana README and the affected `docs/` pages are updated in the same commits as the API, config and permission changes. If `admin/` changes, `check-admin-openapi.sh` and the committed `modules/boardwalk/dist` gate (`check-admin-dist.sh`) apply.
### Post-research decisions (2026-10-21, plan-phase)
- **D-19:** **No outranking.** A non-superuser with the manage-administrators permission is refused (403) when updating, deleting or running record/bulk actions on a non-superuser admin whose effective role + permissions grant more than the caller holds (closes the password-set account-takeover path).
- **D-20:** **Own password is not set on this screen.** Sending a password for your own record on the Administrators form is refused; the error points to My Account, which checks the current password.
- **D-21:** **SPA support is in scope.** D-06 (permission override disabled while `is_superuser` is on) gets a real field `trigger` in fields.yaml and the admin SPA, and D-16 (code-declared role grants) gets per-record locked permission values rendered as locked in the permission editor. Server-side enforcement stays authoritative. The `admin/` change regenerates `admin/openapi/admin.json` + `schema.d.ts` (`scripts/check-admin-openapi.sh`) and rebuilds the committed `modules/boardwalk/dist` (`scripts/check-admin-dist.sh`).
- **D-22:** **Three plans:** 01 the pact action-extension seam, the internal cabana pseudo-plugin with Administrators + Roles controllers, privilege guards, lang keys and docs (server side); 02 a short plan for the SPA support in D-21; 03 unit + Postgres tests (last, per project rule).
### Claude's Discretion
- How the internal pseudo-plugin is shaped and named (id, for example `summercms.cabana` / `backend.*` controller IDs), as long as it reuses the existing `summercms.cabana` migration id and doesn't collide with the reserved first path segments.
- Exact Go identifiers for the D-14 capability and its types.
- List columns and filters (Winter's set: login, name, email, role, last login, activated; filters: role, superuser for superusers only, trashed), and form tab layout.
- The lock strategy for D-09.
- Whether toolbar and bulk actions join the D-14 seam in this phase.
</decisions>
<canonical_refs>
## Canonical References
**Downstream agents MUST read these before planning or implementing.**
### Phase scope and consumers
- `.planning/ROADMAP.md` § "Phase 19.6.1: Backend Administrators screen": goal and success criteria
- `.planning/phases/19.7-generic-mcp-module/19.7-CONTEXT.md`: D-03 to D-07 and D-34 (the link record action lands on this screen through the D-14 seam)
- `.planning/notes/2026-10-17-generic-mcp-module.md`: design note behind 19.7's use of this screen
- `.planning/phases/19.6-two-factor-auth-and-device-flow-in-sm-user-plugin/19.6-CONTEXT.md`: D-15 ("Reset 2FA" action contributed to another plugin's screen, a candidate user of the D-14 seam)
- `.planning/notes/2026-10-07-embedded-admin-release-gate.md`: rebuild and commit the dist if `admin/` changes
### cabana internals to extend
- `modules/cabana/README.md`: module docs to update
- `modules/cabana/contracts.go`: `BackendUser`, `BackendUserRole`, `Allows`, `requiredOf`
- `modules/cabana/auth.go`: `FindByID` permission merge (`principalFrom`, `applyUserPermissions`)
- `modules/cabana/registry.go`: `compilePermissionCatalog`, `rolePermissions`, reserved path segments, controller ID rules
- `modules/cabana/field_permission.go`: `permissioneditor`, `PermissionEditorProvider`, `PermissionOption.Locked`
- `modules/cabana/extension.go`: `compileExtension` (actions only come from the owning controller today)
- `modules/cabana/crud.go`: form lifecycle hooks, `offeredRecordActions`, `RecordAction`, row locks
- `modules/cabana/actions.go`: action run paths and diary logging
- `modules/cabana/http.go`: `Activate`, route mounting, account endpoints
- `modules/cabana/account.go`: My Account allow-list (stays unchanged)
- `modules/cabana/commands.go`, `modules/cabana/token_commands.go`: `admin:*` commands (unchanged behaviour)
- `modules/lagoon/backend_admin_migrations.go`: `backend_users` and `backend_user_roles` schema and seeded system roles
- `pact/capabilities.go`: `HasPermissions`, `HasAdminControllers`, `HasSettings`, `AdminRecordAction`, `HasAdminRecordActions`, form hooks
### Reference implementations
- `../sm-grzybyfunkcjonalne-app/plugins/golem15/user/controllers/users_admin_controller.go`: Users screen with radio permission editor, bulk and record actions
- `../sm-grzybyfunkcjonalne-app/plugins/golem15/user/classes/privileged.go`: privileged-group guard in FormBeforeUpdate/Delete, re-reading the row in the tx
- `../examples/golem15-wintercms-starter/modules/backend/controllers/Users.php`: superuser filtering, per-user PermissionEditor, restore
- `../examples/golem15-wintercms-starter/modules/backend/controllers/UserRoles.php`
- `../examples/golem15-wintercms-starter/modules/backend/models/user/fields.yaml`, `../examples/golem15-wintercms-starter/modules/backend/models/userrole/fields.yaml`
- `../examples/golem15-wintercms-starter/modules/backend/models/UserRole.php`: system roles
- `../examples/golem15-wintercms-starter/modules/backend/formwidgets/PermissionEditor.php` (lines ~93-150): `getSaveValueSecure` ceiling
</canonical_refs>
<code_context>
## Existing Code Insights
### Reusable Assets
- Generic SPA renderer (`admin/src/views/ListView.vue`, `FormView.vue`, `PreviewView.vue`, `components/form/FieldRenderer.vue`): a YAML-declared controller renders without new Vue code.
- `admin/src/components/form/fields/PermissionEditorField.vue` already handles `Locked` options.
- `admin/src/components/form/RecordActions.vue` renders record actions on the preview screen.
- `permissioneditor` field (radio and checkbox modes) with per-request options from `PermissionEditorProvider`, which can depend on who is asking. That is the D-07 hook.
- Form lifecycle hooks (`FormBeforeCreate/Update/Delete`, `FormRules`, `ListExtendQuery`, `FormExtendQuery`) run inside the save transaction with the row locked `FOR UPDATE`. `cabana.TxFromContext` is available.
### Established Patterns
- Before-update hooks see the already-filled model. To compare with stored values (self-edit, demotion), re-read the row in the tx, as sm-user-plugin's `guardCredentials` does.
- Permission merge order: role JSON, then code-declared role grants, then user overrides (1 grants; -1 or 0 removes). Superusers bypass `Allows` except under scoped API tokens.
- Soft delete via `gorm.DeletedAt`. `tokens_valid_after` revocation, as used by `admin:reset-password`.
- Action runs are diary-logged with `admin_id` (bulk and record).
### Integration Points
- cabana registers no controllers or permissions today, and `Activate` returns nil when no plugin has controllers. An internal pseudo-plugin added to the `plugins` slice can supply the controllers, permissions, settings entries and an embedded AdminFS. With it, the admin routes always mount. The researcher confirms nothing depends on "no controllers means no admin routes".
- The D-14 seam merges into `compileExtension` (`extension.go`), next to the owning controller's actions.
- Admin OpenAPI: swag annotations in cabana regenerate `admin/openapi/admin.json` and `admin/src/api/schema.d.ts` (`scripts/check-admin-openapi.sh`).
</code_context>
<specifics>
## Specific Ideas
- Mirror WinterCMS Settings → System → Administrators / Roles in look and behaviour, except where the decisions above deliberately tighten it: the role-assignment ceiling (D-07), last-superuser protection (D-09), and self-edit refusal (D-08).
- Winter has no last-superuser rule; D-09 is new.
</specifics>
<deferred>
## Deferred Ideas
- Invite or welcome mail on admin create (Winter's `send_invite`): a later todo.
- A "send password reset link" record action and force-change-on-next-login for backend admins.
- Per-row action menus on list views.
- Backend user groups (Winter's groups tab). No table exists.
- Impersonate admin (Winter's `backend.impersonate_users`).
### Reviewed Todos (not folded)
- **Audit-log admin action runs with the admin id** (`.planning/todos/pending/admin-action-audit-log.md`): kept deferred to keep the phase short. Record and bulk runs already diary-log `admin_id`.
- **Verified link between a frontend user and a backend user** (`.planning/todos/pending/2026-10-17-frontend-backend-account-link.md`): already folded into 19.7 (D-01). This phase only provides its seam.
- The other keyword matches (scaffolding, plugin ports, i18n, benchmarks) were unrelated to this phase.
</deferred>
---
*Phase: 19.6.1-backend-administrators-screen*
*Context gathered: 2026-10-21*

View File

@@ -0,0 +1,56 @@
# Phase 19.6.1: Backend Administrators screen - Discussion Log
> **Audit trail only.** Do not use as input to planning, research, or execution agents.
> Decisions are captured in CONTEXT.md — this log preserves the alternatives considered.
**Date:** 2026-10-21
**Phase:** 19.6.1-backend-administrators-screen
**Areas discussed:** Permissions and placement, Privilege edge rules, Lifecycle and passwords, Record-action seam and roles
---
## Permissions and placement
| Question | Options | Selected |
|----------|---------|----------|
| Permission codes | Two codes (`backend.manage_users` + `backend.manage_roles`) / One code as in Winter / Superuser only | Two codes |
| Placement | Settings → System section / Top-level navigation item | Settings → System |
| Superusers for non-superusers | Hidden as in Winter / Visible read-only | Hidden |
| Default role grant | None / Grant to `developer` system role | None |
## Privilege edge rules
| Question | Options | Selected |
|----------|---------|----------|
| Per-user permission override on the form | Yes, radio editor / No, roles only | Yes, radio |
| Reach of the "can't grant more" ceiling | Permissions and role assignment / Permission editor only (Winter) | Permissions and role assignment |
| Self-edit | Allowed with privileged fields read-only / Redirect to My Account | Allowed, read-only privileged fields |
| Last-superuser enforcement | Every write path, locked / Admin screen only | Every write path |
## Lifecycle and passwords
| Question | Options | Selected |
|----------|---------|----------|
| Removal | Deactivate + soft delete + restore / Deactivate only / Deactivate + hard delete | Deactivate + soft delete + restore |
| Password set/reset | Typed in form / Form + reset-link action / Form + force change | Typed in form |
| Revocation on deactivate/delete/password set | Bump `tokens_valid_after` / Principal check only | Bump |
| Invite mail on create | None this phase / Optional `send_invite` | None |
## Record-action seam and roles
| Question | Options | Selected |
|----------|---------|----------|
| Seam shape | Generic cross-controller extension / Administrators-only hook | Generic |
| Where actions appear | Preview only / Preview + per-row list menu | Preview only |
| Fold admin-action audit-log todo | No, keep deferred / Fold in | Keep deferred |
| Roles: system roles and delete in use | Winter rules + block delete in use / Winter rules + unassign on delete | Block delete in use |
The user chose the recommended option in every case and added no free-text notes.
## Claude's Discretion
- Shape and name of the internal pseudo-plugin, Go identifiers of the seam, list columns and filters, the D-09 lock strategy, and whether toolbar and bulk actions join the seam.
## Deferred Ideas
- Invite mail, reset-link action, force-change-on-next-login, per-row list actions, backend groups, impersonation, and the admin-action audit log.

View File

@@ -0,0 +1,371 @@
# Phase 19.6.1: Backend Administrators screen - Pattern Map
**Mapped:** 2026-10-21
**Files analyzed:** 24 (new + modified, grouped by plan per D-22)
**Analogs found:** 22 / 24
Primary template for both screens: sm-user-plugin's Users screen. Paths below are relative to the meta repo (`/media/nvme/dev/golem15/summercms.io/summercms/`). The plugin is the git submodule at `sm-grzybyfunkcjonalne-app/plugins/golem15/user`, abbreviated **`USER/`** below. Every file named was checked with `git ls-files` inside that submodule (tracked). Framework paths are relative to `summercms.go/`.
## File Classification
| New/Modified File | Role | Data Flow | Closest Analog | Match Quality |
|---|---|---|---|---|
| **Plan 01: server** | | | | |
| `modules/pact/capabilities.go` (+ `AdminActionExtension`, `HasAdminActionExtensions`) | contract | boot-time registration | `pact.HasAdminRecordActions` / `HasAdminControllers` (same file, lines 183-190, 343-376) | exact |
| `modules/cabana/action_extensions.go` (new, `compileActionExtensions`) | compiler | boot-time merge | `modules/cabana/extension.go` `compileExtension` (lines 60-110) | exact |
| `modules/cabana/http.go` (`Activate`: conditional pseudo-plugin, merge call, `screens.reg = reg`; `formSchema`/list schema per-request field drops) | wiring | request-response | itself, lines 74-130 and 753-790 | exact (in-place) |
| `modules/cabana/backend_screens.go` (new pseudo-plugin) | plugin/provider | boot | `USER/admin.go` (lines 1-38) + `modules/cabana/example_controller_test.go` `BlogPlugin` (lines 115-135) | role-match |
| `modules/cabana/backend_users_admin.go` (new, `summercms.cabana.users`) | controller | CRUD + actions | `USER/controllers/users_admin_controller.go` | exact |
| `modules/cabana/backend_roles_admin.go` (new, `summercms.cabana.roles`) | controller | CRUD | `USER/controllers/users_admin_controller.go` (guardCredentials re-read, FormBeforeDelete, permission provider) | role-match |
| `modules/cabana/backend_privileges.go` (new: `actsAsSuperuser`, `guardLastSuperuser`, grant-subset / outranks helpers) | utility | transform + tx lock | `USER/.../users_admin_controller.go` `guardPrivilegedMember` (193-209); `cabana.Allows`/`granted` in `modules/cabana/contracts.go:214-248` | role-match |
| `modules/cabana/contracts.go` (`BackendUser`/`BackendUserRole` gain `Fillable`, `Rules`, `FilterScopes`, `FilterScope`, unexported snapshot field) | model | CRUD | `Post.Fillable` in `modules/cabana/example_controller_test.go:25-28` | role-match |
| `modules/cabana/crud.go` (unexported projection hook in `projectFullRecord`, record permission locks in meta) | service | request-response | `projectFullRecord` (crud.go:647-670), `FormRecordTitle` hook use at 660-667 | exact (in-place) |
| `modules/cabana/field_permission.go` (skip record-locked codes in the locked comparison; record-lock provider) | service | request-response | itself, lines 55-65 (provider interface), 290-312 (locked check) | exact (in-place) |
| `modules/cabana/relation_field.go` `RecordMeta` (+ `PermissionLocks`) | DTO | response | `RecordMeta` (relation_field.go:95-102) `Labels`/`Notices` | exact |
| `modules/cabana/form_schema.go` + new `field_trigger.go` (`trigger` key, compile + check) | compiler | boot | `modules/cabana/field_dependent.go` `compileDependsOn` / `checkDependsOn` (lines 23-60) | exact |
| `modules/cabana/schema_types.go` `FormField` (+ `Trigger`) | DTO | schema payload | `FormField.DependsOn` (schema_types.go:278-281) | exact |
| `modules/cabana/backendscreens/controllers/users/{config_list,config_form,config_filter}.yaml`, `_hint.htm` | config | — | `USER/controllers/users/{config_list,config_form,config_filter}.yaml`, `_hint.htm` | exact |
| `modules/cabana/backendscreens/models/user/{columns,fields}.yaml` | config | — | `USER/models/user/{columns,fields}.yaml` | exact |
| `modules/cabana/backendscreens/controllers/roles/*.yaml`, `models/userrole/*.yaml` | config | — | `USER/controllers/usergroups/*`, `USER/models/usergroup/*` (no preview block) | exact |
| `modules/phrasebook/backend/lang/{en,pl}/lang.yaml` (+ `user:`, `user.role:`, `permissions:`, `form.trigger_*`, `permissioneditor.locked_*`, `settings.category_system`) | i18n | — | existing `permissioneditor:` block (en lang.yaml:258-264) | exact |
| `modules/cabana/admin_openapi.go` (swag for `trigger`, `meta.permissionLocks`) | docs/annotation | — | existing `dependsOn` description (reflected in `admin/src/api/schema.d.ts:2203, 5778`) | exact |
| `modules/cabana/README.md`, `docs/backend/users-and-permissions.md` (line ~138 paragraph) | docs | — | same files | in-place |
| **Plan 02: SPA (D-21)** | | | | |
| `admin/src/components/form/trigger.ts` (new) | utility | transform | `admin/src/components/form/control.ts` `permissionValues` (lines 77-90) | role-match |
| `admin/src/components/form/formState.ts` `editablePayload` | utility | transform | itself, lines 47-77 | exact (in-place) |
| `admin/src/components/form/FormField.vue` (trigger note), `fields/PermissionEditorField.vue` (record locks), `FormGrid.vue`/`FieldRenderer.vue` (prop threading), `views/FormView.vue`, `views/PreviewView.vue` | component | render | `labels` prop path FormGrid.vue:20,64 → FormField.vue:18,65 → FieldRenderer.vue:22; lock markup PermissionEditorField.vue:79-84,147-166; note markup RelationField.vue:326-337 | exact |
| **Plan 03: tests** | | | | |
| `modules/cabana/backend_*_test.go` (Postgres) | test | — | `modules/cabana/auth_test.go` `adminGorm` (314-360); `modules/cabana/example_actions_test.go`; `USER/admin_privileged_test.go`, `USER/admin_users_test.go` | exact |
| `admin/tests/form/{trigger,PermissionEditorField,formState,FormView,PreviewView}.test.ts` | test | — | existing `admin/tests/form/PermissionEditorField.test.ts`, `formState.test.ts`, `DependentDropdown.test.ts` | exact |
## Pattern Assignments
### `modules/cabana/backend_users_admin.go` (controller, CRUD + actions)
**Analog:** `USER/controllers/users_admin_controller.go`. Note: this file lives in package `cabana`, so drop the `cabana.` prefix on `TxFromContext`, `VirtualFieldsFromContext`, `ForbiddenError`, `RelationLock`, `PermissionOption`, `FieldRelationContract`, `Allows`.
**Controller identity + lazy app accessor** (lines 37-60):
```go
type usersAdminController struct {
app func() *backpack.App
}
func (usersAdminController) ID() string { return "golem15.user.users" } // -> "summercms.cabana.users"
func (usersAdminController) ModelName() string { return `Golem15\User\Models\User` }
func (usersAdminController) ConfigDir() string { return "controllers/users" }
func (usersAdminController) RequiredPermissions() []string { return []string{PermissionAccessUsers} } // -> backend.manage_users
func (usersAdminController) NewRecord() any { return &models.User{} } // -> &BackendUser{}
```
**Unscoped list/form query** (lines 85-97) — extend with D-03 `Where("backend_users.is_superuser = ?", false)` when `!actsAsSuperuser(principal)`:
```go
func (usersAdminController) ListExtendQuery(_ context.Context, db *gorm.DB) *gorm.DB {
if db == nil { return db }
return db.Unscoped()
}
```
**Row states** (lines 102-129): copy the loop, keep only `pact.RowStateDeleted` (`DeletedAt.Valid`) and `pact.RowStateDisabled` (`!IsActivated`); no throttle query.
**Virtual fields + rules** (lines 135-155):
```go
func (usersAdminController) FormVirtualFields() []string {
return []string{"password", "password_confirmation", "send_invite"} // -> password, password_confirmation, is_superuser
}
func (c usersAdminController) FormRules(_ context.Context, op string) map[string]string {
password := "nullable|" + length + "|confirmed"
if op == "create" { password = "required|" + length + "|confirmed" }
...
}
```
Use `min:4` (Winter) and add the 72-byte check (`passwordMaxBytes`, `modules/cabana/account.go:46-47, 335-336`). Do NOT use lagoon `unique` for login/email (Research Pitfall 4).
**Password storing + revocation** (lines 303-321, 238-247):
```go
values, _ := cabana.VirtualFieldsFromContext(ctx)
plain, _ := values["password"].(string)
if plain == "" { return false, nil }
hashed, err := bouncer.HashPassword(c.configInt(...), plain) // -> bouncer.HashPassword(adminBcryptCost(app), plain)
...
if changed { now := time.Now(); user.TokensValidAfter = &now }
```
Use the `admin:reset-password` cutoff verbatim (`modules/cabana/commands.go:157`): `cutoff := time.Now().UTC().Add(time.Second)`. D-20: own record + non-empty password -> `&ForbiddenError{Message: "backend::lang.user.own_password", Details: map[string]any{"password": []string{...}}}`.
**Create defaults** — copy `admin:create` (`modules/cabana/commands.go:106-115`): `IsActivated: true, ActivatedAt: &now`. Uniqueness query to mirror (commands.go:96), unscoped, excluding own id:
```go
tx.Model(&BackendUser{}).Where("login IN (?, ?) OR lower(email) IN (?, ?)", login, email, strings.ToLower(login), email).Count(&existing)
```
**Re-read stored row in the tx** (lines 479-497) — template for the prior-state snapshot / role-code check:
```go
err := db.Session(&gorm.Session{NewDB: true}).WithContext(ctx).Unscoped().
Model(&models.User{}).Where("id = ?", user.ID).Pluck("email", &stored).Error
```
**Privilege guard returning 403 with field details** (lines 193-209, 499-522):
```go
principal, _ := bouncer.User(ctx)
if cabana.Allows(principal, []string{classes.PermissionManagePrivilegedGroups}) { return nil }
...
return &cabana.ForbiddenError{Message: message, Details: details}
```
Every delete/action refusal must set `Message` (UI-SPEC C8).
**Writable protected FK relation** (lines 370-380) for `role`:
```go
{Field: "organisation", Kind: "belongsTo", NewRelated: func() any { return &models.Organisation{} },
ForeignKey: "organisation_id", LabelColumn: "name", WritableForeignKey: true},
// -> Field "role", NewRelated &BackendUserRole{}, ForeignKey "role_id"
```
**Role ceiling via relation locks** (lines 402-422):
```go
func (c usersAdminController) AdminRelationLocks(ctx context.Context, field string) (cabana.RelationLock, error) {
if field != groupsField { return cabana.RelationLock{}, nil } // -> "role"
principal, _ := bouncer.User(ctx)
if cabana.Allows(principal, ...) { return cabana.RelationLock{}, nil } // -> actsAsSuperuser
...
return cabana.RelationLock{IDs: ids, Message: usersLang + "privileged_group_forbidden"}, nil // -> backend::lang.user.role_ceiling
}
```
**Permission editor provider** (lines 426-477): copy the three methods; options come from the registry catalog (ordered) with `Locked: !actsAsSuperuser(p) && !granted(p.PermissionGrants, code)`; `AdminSetPermissionValues` refuses own record (D-08) and writes `BackendUser.Permissions` JSON.
**Delete hooks** (lines 524-536): `FormBeforeDelete` is called once per row in bulk delete (rollback of whole selection). Analog's `FormAfterDelete` (538-556) hard-deletes — **do not copy that part**; ours only bumps `tokens_valid_after` via `tx.Session(&gorm.Session{NewDB: true}).WithContext(ctx).Unscoped()` (D-10, D-12).
**Bulk actions** (lines 589-627) and **record actions** (lines 665-743): copy the `bulk(...)` / `action(...)` builder closures verbatim, including:
```go
tx, ok := cabana.TxFromContext(ctx)
if !ok { return pact.AdminRecordActionResult{}, errors.New("users admin: a record action needs its transaction") }
```
Names: `activate`, `deactivate`, `restore` (both record and bulk). Deactivate: self -> 403 `user.self_deactivate`, `guardLastSuperuser`, outranks (D-19), bump cutoff.
**Preview hint** (lines 746-770 + `USER/controllers/users/_hint.htm`): copy `usersHintView{Kind, Title, Text}` + `PartialData(ctx, "hint", record)`; precedence deleted > deactivated > own account.
---
### `modules/cabana/backend_roles_admin.go` (controller, CRUD)
**Analog:** same file. Reuse: identity block, `FormRules` (`name` required+unique, `code` uniqueness by hand -> `user.role.code_taken`), re-read pattern (lines 479-497) to detect a `code` change on a system role (`code` is fillable, already written before `FormBeforeUpdate`), `FormBeforeDelete` shape (524-536) returning `ForbiddenError{Message: "backend::lang.user.role.delete_has_users"}` after an **unscoped** `Count` of `backend_users` by `role_id`. Permission provider: checkbox mode; record locks = `reg.rolePermissions(role.Code)` (registry.go:449-460). No preview block, no record actions (YAML from `USER/controllers/usergroups/`).
---
### `modules/cabana/backend_screens.go` (pseudo-plugin)
**Analog:** `USER/admin.go` lines 11-38:
```go
//go:embed controllers/users/config_list.yaml controllers/users/config_form.yaml controllers/users/config_filter.yaml controllers/users/_hint.htm models/user/fields.yaml models/user/columns.yaml
var adminFS embed.FS
func (p *Plugin) AdminFS() fs.FS { return adminFS }
func (p *Plugin) AdminControllers() []pact.AdminController {
return controllers.AdminControllers(func() *backpack.App { if p == nil { return nil }; return p.app })
}
var (
_ pact.AdminAssets = (*Plugin)(nil)
_ pact.HasAdminControllers = (*Plugin)(nil)
_ pact.HasPermissions = (*Plugin)(nil)
)
```
Embed root: `backendscreens/` (use `fs.Sub` so paths start at `controllers/...`). Add `party.Plugin` methods (`ID() "summercms.cabana"`, `Requires() nil`, no-op `Register`/`Boot`, `modules/party/registry.go:16-21`), `pact.HasSettings`.
**Settings link entries** — analog `pact.SettingsItem` with `Controller` (capabilities.go:570-590; link entry declares no Model/Form/NewModel). Settings item fixture shape: `modules/cabana/example_controller_test.go:121-133`:
```go
return []pact.SettingsItem{{Code: "blog", Label: "...", Description: "...", Category: "...", Icon: "...", Permissions: []string{...}, ...}}
// -> {Code: "administrators", Controller: "summercms.cabana.users", Permissions: []string{"backend.manage_users"},
// Label: "backend::lang.user.menu_label", Description: "backend::lang.user.menu_description",
// Category: "backend::lang.settings.category_system", Icon: "users", Order: 100}
// {Code: "roles", Controller: "summercms.cabana.roles", ..., Icon: "shield-check", Order: 110}
```
Permissions: `pact.Permission{Code, Tab, Label, Roles}` with `Roles` empty (D-04).
---
### `modules/cabana/http.go` `Activate` (wiring)
**Analog:** itself, lines 74-130. Insertion points:
```go
items, err := collectControllers(plugins)
...
if len(items) == 0 { return nil, nil } // line 79: keep; append pseudo-plugin AFTER this check, then re-collect
...
reg, err := compileRegistryWithTypes(items, fieldTypes) // line 103
// NEW: compileActionExtensions(reg, plugins) -- must run before compileContributions (validates action perms)
if err := compileContributions(reg, plugins); err != nil { ... } // line 125
```
Per-request schema filter analog (http.go:767-785): the widget-drop loop builds a fresh `kept` slice — add the D-03 drop of `is_superuser` there through an unexported interface; mirror for the list schema columns and filters.
---
### `modules/cabana/action_extensions.go` (D-14 merge)
**Analog:** `modules/cabana/extension.go` `compileExtension` lines 60-110:
```go
recordActions, err := compileRecordActions(cc.Controller)
if err != nil { return fmt.Errorf("cabana: admin controller %s/%s: %w", pluginID, id, err) }
cc.RecordActions = recordActions
...
if len(cc.Form.recordActions) > 0 && cc.Form.preview == nil {
return bootErr(pluginID, id, formFile, fmt.Errorf("recordActions needs a preview block (record actions are offered on the preview screen)"))
}
for _, name := range cc.Form.recordActions {
action, ok := recordActions[name]
...
if strings.TrimSpace(action.Label) == "" { return bootErr(..., fmt.Errorf("recordActions: action %s needs a label", name)) }
}
```
Merge = append name to `cc.Form.recordActions` and action to `cc.RecordActions`; errors for unknown controller ID, no form/preview, duplicate name, non-identifier, missing `Run`/`Label` (reuse validation of `compileRecordActions`, extension.go:401-427). Consumers then work unchanged: `offeredRecordActions` (crud.go:551-575) and `recordActionOf` (actions.go:242-247) iterate `cc.Form.recordActions`.
### `modules/pact/capabilities.go`
**Analog** (lines 186-190, 372-376):
```go
// HasAdminControllers is implemented by plugins that register admin controllers.
type HasAdminControllers interface { AdminControllers() []AdminController }
// HasAdminRecordActions is implemented by an admin controller that registers ...
type HasAdminRecordActions interface { AdminRecordActions() []AdminRecordAction }
```
New: `type AdminActionExtension struct { Controller string; RecordActions []AdminRecordAction }` + `type HasAdminActionExtensions interface { AdminActionExtensions() []AdminActionExtension }` (plugin-level). Reuse `AdminRecordAction` (343-354) unchanged.
---
### `modules/cabana/field_trigger.go` + `form_schema.go` + `schema_types.go` (D-21 trigger)
**Analog:** `modules/cabana/field_dependent.go` lines 23-60:
```go
func compileDependsOn(typ string, values map[string]ast.Node, field *FormField) error {
node, ok := values["dependsOn"]
if !ok { return nil }
...
name, err := nodeString(node)
if err != nil || !identifier(name) { return fmt.Errorf("dependsOn %q is not a field name", nodeText(node)) }
field.DependsOn = name
return nil
}
// checkDependsOn(fields []FormField, owner string) error -- byName map, sibling must exist and be scalar
```
Register `"trigger": {}` in `formFieldKeys` (form_schema.go:38-50); call site next to `compileDependsOn` (form_schema.go:646). Payload field next to `DependsOn string \`json:"dependsOn,omitempty"\`` (schema_types.go:278-281). Refuse `trigger` inside repeater forms; controlling field must be `switch`/`checkbox`.
### `modules/cabana/crud.go` / `field_permission.go` / `relation_field.go` (projection + record locks)
**Projection hook analog** — `FormRecordTitle` use inside `projectFullRecord` (crud.go:660-667):
```go
if hook, ok := cc.Controller.(pact.FormRecordTitle); ok && hook != nil {
title, err := hook.FormRecordTitle(ctx, tx, model)
if err != nil { return RecordResult{}, lifecycleFailure(cc, err) }
meta.Title = title
}
```
Use an **unexported** interface (only cabana's controller can implement) to add `data["is_superuser"]` and `meta.PermissionLocks`. Protected key list to leave untouched: crud.go:1407-1417.
**Locked comparison to amend** (field_permission.go:303-307) — skip record-locked codes:
```go
for _, option := range options {
if option.Locked && stored[option.Code] != submitted[option.Code] {
return &ForbiddenError{Details: map[string]any{value.field: []string{permissionLockedKey}}}
}
}
```
**Meta DTO** (relation_field.go:95-102): add `PermissionLocks map[string][]PermissionLock \`json:"permissionLocks,omitempty"\`` beside `Labels`/`Notices`.
---
### Admin YAML (`modules/cabana/backendscreens/...`)
**Analog:** `USER/controllers/users/config_list.yaml`, `config_form.yaml`, `config_filter.yaml` (read in full). Copy structure; substitutions per UI-SPEC C2/C3/C5:
```yaml
list: ~/plugins/golem15/user/models/user/columns.yaml # -> path inside backendscreens FS (check how ~/ resolves for the pseudo-plugin)
recordUrl: golem15/user/users/preview/:id # -> summercms/cabana/users/preview/:id
toolbar: { buttons: [create, delete], search: { prompt: backend::lang.list.search_prompt } }
bulkActions: [activate, deactivate, restore]
messages: { rowStateDeleted: ..., rowStateDisabled: ... } # no rowStateNegative
```
```yaml
preview:
headerPartial: hint
recordActions: [activate, deactivate, restore]
create: { redirect: .../preview/:id, redirectClose: ... }
update: { redirect: .../update/:id, redirectClose: .../preview/:id }
```
Filters: copy `scope` (groups → role, `modelClass`, `nameFrom`, `scope`), `switch` (activated), `daterange` (created_date → last_login). Trashed = `scope` with two `FilterOptions`.
`_hint.htm` copy verbatim:
```html
{{- if .Data.Title -}}
<div class="summer-callout summer-callout--{{ .Data.Kind }}" role="status">
<p class="summer-callout__title">{{ trans .Data.Title }}</p>
<p class="summer-callout__text">{{ trans .Data.Text }}</p>
</div>
{{- end -}}
```
---
### SPA (Plan 02)
**`formState.ts` `editablePayload`** (lines 53-77) — add a trigger-active skip before the permissioneditor branch:
```ts
for (const field of fields) {
if (field.readOnly || !isRegisteredField(field)) { continue }
...
if (field.type === 'permissioneditor') {
out[field.name] = permissionValues(field, value) // record-locked codes must be skipped here too
continue
}
```
**`control.ts` `permissionValues`** (77-90): pass record locks, skip their codes.
**`PermissionEditorField.vue`** (lines 15-28, 79-84, 147-166, 184-207): extend `isDisabled` (line 79 `readOnly.value || !!option.locked`) and `showsLock` (83-84) with record locks; note line replaces `t('backend::lang.permissioneditor.locked')` with the record note; add `data-locked-by="record"`.
**Prop threading:** copy `labels` path — `FormGrid.vue:20` (`labels?: RecordMeta['labels']`) → `:labels="labels?.[field.name]"` (64) → `FormField.vue:18,65` → `FieldRenderer.vue:22`.
**Trigger note markup:** copy RelationField locked note (RelationField.vue:326-337, `Lock :size="14" class="shrink-0 text-muted"`), swap to `Info`, `flex items-center gap-2 text-[13px] text-muted`.
**Gates:** `scripts/check-admin-openapi.sh`, rebuild + commit `modules/boardwalk/dist`, `scripts/check-admin-dist.sh`.
---
### Tests (Plan 03)
**Postgres helper:** `modules/cabana/auth_test.go:320-360`:
```go
func adminGorm(t *testing.T) *gorm.DB {
t.Helper()
if testing.Short() { t.Skip("requires testcontainers postgres") }
adminOnce.Do(func() { ... postgres.Run(ctx, "postgres:16-alpine", postgres.WithDatabase("cabana"), ...) })
```
**Privilege test analog:** `USER/admin_privileged_test.go`, `USER/admin_users_test.go`, harness `USER/admin_harness_test.go`. **Record action run-path analog:** `modules/cabana/example_actions_test.go`, `phase121_record_test.go`. **Boot error analog:** `modules/cabana/phase121_schema_boot_test.go`. Also re-run `cd examples/hello && go test ./...` and `go test ./modules/surf` (Activate no-controllers invariant).
**SPA:** `admin/tests/form/PermissionEditorField.test.ts`, `formState.test.ts`, `FormView.test.ts`, `PreviewView.test.ts`, `RecordActions.test.ts`, `DependentDropdown.test.ts` (sibling-field evaluation analog for trigger).
## Shared Patterns
### Write transaction access
**Source:** `USER/controllers/users_admin_controller.go:543-546, 600-603`
**Apply to:** every hook/action that writes or counts (incl. `guardLastSuperuser`)
```go
tx, ok := cabana.TxFromContext(ctx)
if !ok { return errors.New("...: a delete needs its transaction") }
db := tx.Session(&gorm.Session{NewDB: true}).WithContext(ctx).Unscoped()
```
### Refusals
**Source:** `ForbiddenError` use at users_admin_controller.go:208 and field_permission.go:305
**Apply to:** D-05/07/08/09/17/19/20. 403 with `Message` (toast/banner) + `Details` keyed by field (field line). 422 `ValidationError` only for login/email/code field errors.
### Principal + permission checks
**Source:** `bouncer.User(ctx)` + `Allows(principal, codes)` (users_admin_controller.go:194-197; contracts.go:214-248). `actsAsSuperuser` = `p != nil && p.IsSuperuser && (p.Scope == nil || p.Scope.Full)` (bouncer/context.go:16-35). Never pass a wildcard role grant to `granted`.
### Revocation cutoff
**Source:** `modules/cabana/commands.go:157-162` — `time.Now().UTC().Add(time.Second)` into `tokens_valid_after`. Apply to deactivate, delete, password set for another admin; not to restore/activate.
### Lang keys
**Source:** `modules/phrasebook/backend/lang/en/lang.yaml` top-level groups (`permissioneditor:` at 258). Add en + pl in the same commit; `validateMessageKeys` (messages.go:296-315) fails boot on missing `messages.*` keys. CLDR plural maps, not pipe strings.
## No Analog Found
| File / mechanism | Role | Data Flow | Reason |
|---|---|---|---|
| `guardLastSuperuser` with `pg_advisory_xact_lock` (in `backend_privileges.go`) | utility | tx lock | No advisory-lock usage in the repo; use RESEARCH.md "The last-superuser guard (skeleton)" |
| Cross-controller merge semantics (unknown target / duplicate across plugins) | compiler | boot | Only owner-controller actions exist; error wording follows `bootErr` in extension.go |
## Metadata
**Analog search scope:** `summercms.go/modules/{cabana,pact,party,phrasebook}`, `summercms.go/admin/src/components/form`, `summercms.go/admin/tests/form`, `sm-grzybyfunkcjonalne-app/plugins/golem15/user` (tracked submodule).
**Files scanned:** ~30
**Pattern extraction date:** 2026-10-21

View File

@@ -0,0 +1,588 @@
# Phase 19.6.1: Backend Administrators screen - Research
**Researched:** 2026-10-21
**Domain:** cabana (admin framework) — generic CRUD reuse, server-side privilege rules, a new `pact` capability
**Confidence:** HIGH for in-repo mechanics (every hook, ordering and value below was read from source this session); MEDIUM for the recommended shapes where CONTEXT leaves discretion; LOW items are listed in the Assumptions Log
<user_constraints>
## User Constraints (from CONTEXT.md)
### Locked Decisions
#### Permissions and placement
- **D-01:** The screens are guarded by **two new permission codes**:
- `backend.manage_users` for Administrators (Winter's name is kept);
- `backend.manage_roles` for Roles.
Roles change what every holder can do, so they get their own code. These are the first permissions cabana itself registers, through an internal pseudo-plugin (see Code Insights). — **Reversibility:** costly — the permission codes end up stored in `backend_user_roles.permissions` and `backend_users.permissions` JSON on every install that grants them.
- **D-02:** **Placement: Settings → System section**, as in Winter, through `pact.HasSettings` entries that link into the controllers. No top-level navigation item.
- **D-03:** **Superusers are hidden from non-superusers**, as in Winter:
- `ListExtendQuery` and `FormExtendQuery` filter `is_superuser` rows out;
- the `is_superuser` field and filter are removed for non-superusers;
- opening a superuser record by ID answers 404 or 403.
- **D-04:** **No default role grant.** Neither code is added to the `developer` or `publisher` system roles through code-declared `Roles`. Only superusers reach the screens until someone grants the codes, so existing installs don't widen silently on upgrade.
#### Privilege rules (server-side, all enforced in the save transaction)
- **D-05:** **Only a superuser can create or edit a superuser, or set `is_superuser`** (roadmap SC-2). A non-superuser sending `is_superuser` is answered 403, even if the field is hidden.
- **D-06:** **The per-user permission override is editable**: a permissions tab on the Administrators form with `permissioneditor` mode **radio** (inherit / allow / deny), as in Winter. It is disabled when `is_superuser` is on. The column and its merge (`applyUserPermissions`, merged last) already exist.
- **D-07:** **A non-superuser can't grant more than they hold**, and this covers both permissions and role assignment.
- **Permission editors**, on both the per-user tab and the Roles screen: an option the editor doesn't hold is `PermissionOption.Locked`, and its stored value is kept on save (Winter's `getSaveValueSecure`). Changing a locked value is answered 403, the existing behaviour.
- **Role picker**: it offers only roles whose merged grants (stored JSON plus code-declared grants) are a subset of the editor's own permissions. Assigning any other role is refused on the server.
- **Roles screen**: a non-superuser can't add a permission they lack to any role.
- **D-08:** **Editing your own record is allowed, but privileged fields are read-only.**
- The admin can edit their profile fields on the Administrators screen.
- The server refuses, for the editor's own record, any change to `role_id`, `is_superuser`, `permissions` or `is_activated`, and refuses deleting yourself.
- This covers "an admin cannot raise their own role or permissions" (SC-2) in one place. There is no redirect to My Account.
- **D-09:** **The last active superuser is protected on every write path.** It can't be deactivated, demoted (`is_superuser` off), soft-deleted, or turned inactive by any route. The check covers:
- form update and delete;
- bulk actions, including bulk deactivate and bulk delete;
- record actions;
- any `admin:*` command path that could clear the flag.
It runs inside the save transaction under a lock (for example a row lock on the active superuser set, or an advisory lock), so two concurrent demotions can't both pass. Today's `admin:*` commands don't demote, so their behaviour and output stay unchanged (SC-1).
#### Lifecycle and passwords
- **D-10:** **Deactivate + soft delete + restore.**
- Activate and Deactivate are record and bulk actions that toggle `is_activated`.
- Delete is a soft delete through the existing `deleted_at`.
- A trashed filter lists deleted admins, and a Restore action brings them back.
- No hard delete, so FK references (tokens, `linked_by`, attachments) stay valid.
- **D-11:** **Passwords are typed in the form.** Password and confirmation fields are required on create and optional on update, and blank means unchanged. The value is bcrypt-hashed like `admin:create`. There is no "send reset link" action and no force-change-on-next-login in this phase.
- **D-12:** **Changes cut access immediately.** Deactivate, soft delete, and setting a password for another admin all bump `tokens_valid_after`, as `admin:reset-password` does. Existing JWTs and personal tokens then stop on the next request; `FindByID` already refuses inactive or deleted users. Restore and activate don't bump it.
- **D-13:** **No invite or welcome mail** on create in this phase. Winter's `send_invite` is deferred.
#### Record-action seam
- **D-14:** **A generic cross-controller action extension.** It is a new optional `pact` capability through which a plugin contributes `AdminRecordAction`s to any admin controller by controller ID, including cabana's Administrators controller and other plugins' controllers.
- It may also cover toolbar and bulk actions if the shape allows it at no extra cost (planner's call).
- Contributions merge at boot. A duplicate action name on a controller, or an unknown controller ID, is a boot error.
- Each action keeps its own `Permissions`, `Applies` and `Confirm`. The existing row-locked transactional run path (`crud.go` `RecordAction`) runs it.
- The seam is additive and names neither MCP nor the user plugin. It is the seam 19.7 D-04/D-34 consumes. The researcher checks whether 19.6 D-15 ("Reset 2FA" contributed by sm-mfa-plugin to the user plugin's Users screen) can use it as is.
— **Reversibility:** costly — this is a published `pact` capability that plugins (19.7's link action, sm-mfa-plugin) implement.
- **D-15:** **Contributed actions appear on the preview screen only**, through the existing `RecordActions.vue` on `PreviewView`. The Administrators controller therefore gets a preview block. There are no per-row actions on the list and no list payload changes.
#### Roles screen
- **D-16:** **Fields and system roles follow Winter.**
- Fields: name, code (slugged from name), description, and a permissions tab using `permissioneditor` mode **checkbox**.
- System roles (`is_system`, or roles that code-declared `pact.Permission.Roles` grants attach to) have their code locked, and their code-declared grants are shown as locked.
- Name, description and any extra stored grants stay editable, within the D-07 ceiling.
- **D-17:** **A role that still has admins can't be deleted.** The delete returns a validation error that names how many admins hold the role. System roles can't be deleted.
#### Quality gates
- **D-18:** Unit tests are the last plan (project rule). They cover every privilege rule in D-05 to D-09, including the concurrent last-superuser case, plus D-12 revocation and the D-14 seam (boot errors, permission filtering, run path). Docs: the cabana README and the affected `docs/` pages are updated in the same commits as the API, config and permission changes. If `admin/` changes, `check-admin-openapi.sh` and the committed `modules/boardwalk/dist` gate (`check-admin-dist.sh`) apply.
### Claude's Discretion
- How the internal pseudo-plugin is shaped and named (id, for example `summercms.cabana` / `backend.*` controller IDs), as long as it reuses the existing `summercms.cabana` migration id and doesn't collide with the reserved first path segments.
- Exact Go identifiers for the D-14 capability and its types.
- List columns and filters (Winter's set: login, name, email, role, last login, activated; filters: role, superuser for superusers only, trashed), and form tab layout.
- The lock strategy for D-09.
- Whether toolbar and bulk actions join the D-14 seam in this phase.
### Deferred Ideas (OUT OF SCOPE)
- Invite or welcome mail on admin create (Winter's `send_invite`): a later todo.
- A "send password reset link" record action and force-change-on-next-login for backend admins.
- Per-row action menus on list views.
- Backend user groups (Winter's groups tab). No table exists.
- Impersonate admin (Winter's `backend.impersonate_users`).
#### Reviewed Todos (not folded)
- **Audit-log admin action runs with the admin id** (`.planning/todos/pending/admin-action-audit-log.md`): kept deferred to keep the phase short. Record and bulk runs already diary-log `admin_id`.
- **Verified link between a frontend user and a backend user** (`.planning/todos/pending/2026-10-17-frontend-backend-account-link.md`): already folded into 19.7 (D-01). This phase only provides its seam.
- The other keyword matches (scaffolding, plugin ports, i18n, benchmarks) were unrelated to this phase.
</user_constraints>
## Project Constraints (from CLAUDE.md)
- Keep `go vet ./...` and `go test ./...` green at every commit; `go test -short ./...` is the Docker-free loop. Docker-backed tests skip under `-short` (a `TestMain` must call `flag.Parse()` and check `testing.Short()`). If the Docker socket is denied, run through `sudo -u golem`.
- Standard library first; a new dependency needs a research doc or decision note. **This phase needs none** (see Standard Stack).
- YAML through `goccy/go-yaml`, never `gopkg.in/yaml.v3`.
- A change to a module's exported API, config keys, CLI commands or dependencies updates that module's `README.md` and the affected `docs/` pages **in the same commit**. Every identifier in a README or docs page must exist (the docs checker enforces it: `summer docs:build --check`, `go test ./cmd/summer -run TestDocsTree`).
- Framework code never names a consuming application (neutral names only in READMEs, docs, examples, tests).
- Core plugins (user, blog, pages, payment) must not get breaking changes; the new `pact` capability is additive.
- If `admin/` changes: rebuild, commit `modules/boardwalk/dist`, pass `scripts/check-admin-dist.sh`; swag changes need `scripts/check-admin-openapi.sh`. The recommended design below needs **no `admin/` change** (see Open Questions 3 and 4 for the two places that would).
- Lean planning: few large plans; present the plan count with one-line scopes and wait for confirmation. **Unit tests are the last plan.**
- No co-author tags; one logical change per commit; planning docs and code in separate commits; stage files by path (other sessions edit `STATE.md`/`state.json`; check for in-flight edits before touching cabana). No push/tag/deploy without go-ahead. State via `msd_run query state.*`. Post a g15office comment when the phase advances.
## Summary
Everything the phase needs already exists as a generic cabana seam, with three gaps that must be designed before planning. **What exists:** YAML-driven list/form/preview, `FormVirtualFields` (password, confirmation, and a virtual `is_superuser` switch), `PermissionEditorProvider` with per-request `Locked` options and the stored-value-kept merge (exactly Winter's `getSaveValueSecure`), `RelationLockProvider` (per-request locked related ids, enforced with 403 on save — the right tool for the D-07 role ceiling), `FormExtendQuery`/`ListExtendQuery` with the principal on `ctx` (D-03), row-locked record and bulk action run paths, `ListRowStates`, `FilterScope`/`FilterOptions`, settings link entries (`pact.SettingsItem.Controller`), and `tokens_valid_after` revocation. sm-user-plugin's Users screen is a complete working template for the controller (virtual password, `guardCredentials` re-read in the tx, bulk/record actions, row states).
**The three gaps.** (1) `protectedFillKey` makes `role_id`, `permissions`, `is_superuser`, `is_activated` and `password` unfillable *and unprojected* by the generic CRUD — good for safety, but a virtual `is_superuser` switch would load as "off" on the edit form, and the SPA sends every field, so an untouched save would demote a superuser. cabana must project that value (an unexported in-package hook is enough, because the controller lives in cabana). (2) The save order is `FormBeforeUpdate` → `applyPermissionValues` → `checkRelationLocks` → `assignBelongsTo` → row write → `FormAfterUpdate`, so the new `role_id` is not visible in `FormBeforeUpdate`; the D-08 "no change to your own role" check must compare a snapshot taken in `FormBeforeUpdate` with the model in `FormAfterUpdate`. (3) Contributed record actions (D-14) are not offered or runnable today: `offeredRecordActions` and `recordActionOf` only see names in the owning controller's `config_form.yaml` `recordActions`. The seam must append contributed names to the compiled list at boot.
**Activation caveat:** `Activate` returns `nil` when no plugin registers controllers, and several things depend on it — `examples/hello` boots without `admin.jwt.secret`, and `modules/surf/admin_prefix_test.go` asserts "no admin controllers" → not mounted. Add the internal pseudo-plugin **only when at least one real plugin has admin controllers**; then nothing else changes.
**Primary recommendation:** Two plans. Plan 01: the `pact.HasAdminActionExtensions` seam, the internal `summercms.cabana` pseudo-plugin with `summercms.cabana.users` and `summercms.cabana.roles` controllers over `*cabana.BackendUser`/`*cabana.BackendUserRole`, the privilege guards (one shared `guardLastSuperuser` under `pg_advisory_xact_lock`), lang keys, and README/docs. Plan 02 (last): the unit and Postgres tests for D-05..D-09, D-12, D-14 and the concurrent case.
## Architectural Responsibility Map
| Capability | Primary Tier | Secondary Tier | Rationale |
|------------|-------------|----------------|-----------|
| Administrators/Roles list, form, preview rendering | Browser (generic Vue SPA from `admin/`) | API (cabana schema endpoints) | The SPA renders any YAML-declared controller; no new Vue code is needed |
| Privilege rules D-05..D-09 | API / Backend (cabana controller hooks inside the save tx) | Database (advisory lock, counts in the tx) | Must hold for any client, including personal API tokens and hand-crafted bodies |
| Superuser hiding (D-03) | API (`ListExtendQuery`/`FormExtendQuery`, per-request schema filter) | — | The hidden field is display-only; the query scope is the control |
| Role ceiling and permission ceiling (D-07) | API (`RelationLockProvider`, `PermissionEditorProvider` `Locked`) | Browser (locked rows are display aids) | Framework already refuses locked changes with 403 |
| Last-superuser invariant (D-09) | Database (lock + count inside the write tx) | API (one guard called from every write path) | Only a serialized in-tx check survives concurrency |
| Session revocation (D-12) | Database (`tokens_valid_after`) | API (bouncer JWT check, `FindByID` refusal) | Existing mechanism; JWT guard compares `iat` with the cutoff per request |
| Cross-controller record actions (D-14) | API (boot-time merge in cabana, `pact` capability) | Browser (`RecordActions.vue` on preview, unchanged) | Contributions become ordinary record actions after the merge |
| Permission catalog and settings entries | API (cabana registry, pseudo-plugin) | — | Same path as any plugin's `HasPermissions`/`HasSettings` |
## Standard Stack
No new dependency. Everything is framework code plus the existing set.
### Core
| Library | Version | Purpose | Why Standard |
|---------|---------|---------|--------------|
| Go stdlib | go1.27.2 (local toolchain) | everything | Project rule: stdlib first [VERIFIED: `go version` this session] |
| gorm.io/gorm | v1.31.2 | models, tx, `clause.Locking` | Already the data layer [VERIFIED: summercms.go/go.mod:50] |
| PostgreSQL `pg_advisory_xact_lock(bigint)` | server feature (tests use `postgres:16-alpine`) | D-09 serialization | Built into Postgres; released at tx end [CITED: postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS] |
| bouncer.HashPassword / NeedsRehash | in-repo | bcrypt like `admin:create` | Same cost key `admin.password.bcrypt_cost` [VERIFIED: modules/cabana/commands.go:102, auth.go:481-490] |
### Supporting (test only, already in go.mod)
| Library | Purpose | When to Use |
|---------|---------|-------------|
| testcontainers-go postgres module | Postgres for the privilege tests | Through the existing `adminGorm(t)` helper (skips under `-short`) [VERIFIED: modules/cabana/auth_test.go:314-360] |
### Alternatives Considered
| Instead of | Could Use | Tradeoff |
|------------|-----------|----------|
| `pg_advisory_xact_lock` | `SELECT ... FROM backend_users WHERE is_superuser ... FOR UPDATE` | Deadlock-prone: every write path already holds its target row (`loadRecord`/`lockScoped` take `FOR UPDATE` first), so two demotions of different superusers would each hold one row and wait for the other; Postgres aborts one with a 500 instead of a clean refusal |
| `*cabana.BackendUser` as the record type | unexported wrapper model | A wrapper hides the type from contributors (19.7's link action could only use `RecordID`); `BackendUser` gains a few additive methods instead |
| `RelationLockProvider` for the role ceiling | `RelationExtendOptionsQuery` filtering | Filtering removes the target admin's *current* role from options, so `checkRelationScope` 422s every save of an admin who holds a role above the editor's ceiling, even a name change; locks keep the value, show it locked, and refuse only a change |
**Installation:** none.
## Package Legitimacy Audit
No external packages are installed by this phase. **Packages removed:** none. **Packages flagged:** none.
## Architecture Patterns
### System Architecture Diagram
```
Boot (cabana.Activate)
plugins ──collectControllers──> [none?] ──yes──> return nil (unchanged: no admin, no secret)
│ some
▼
plugins + backendScreens{ID "summercms.cabana"} (internal pseudo-plugin: controllers users/roles,
│ permissions backend.manage_users/manage_roles,
│ 2 settings link entries, embedded AdminFS)
▼
compileRegistryWithTypes ──> compileActionExtensions (NEW: pact.HasAdminActionExtensions;
│ append contributed names to cc.Form.recordActions + cc.RecordActions;
│ unknown controller / no preview / duplicate name => boot error)
▼
compileContributions (permission catalog incl. backend.*, validates contributed action perms)
▼
backendScreens.reg = reg (controllers read the catalog + role grants per request)
Request: PUT /backend/api/v1/summercms/cabana/users/{id}
guard (JWT or g15_ token) ─> protect(): RequiredPermissions backend.manage_users
▼
CRUDService.save tx:
loadRecord (FormExtendQuery: unscoped; non-superuser => is_superuser = false; FOR UPDATE)
Fill (protected keys never filled) ─> Validate (FormRules: password rules)
FormBeforeUpdate ── snapshot prior role_id; virtual is_superuser (D-05, D-08, D-09 guard);
│ password hash + tokens_valid_after (D-11, D-12); login/email uniqueness
applyPermissionValues ─> AdminSetPermissionValues (D-07 locked kept; D-08 self refused)
checkRelationLocks (role ceiling, D-07) ─> assignBelongsTo (role_id)
row write ─> FormAfterUpdate (D-08: self role_id changed vs snapshot => 403, rollback)
▼
projectFullRecord (+ unexported hook adds is_superuser to data)
Record/bulk action: POST .../users/{id}/actions/deactivate or .../bulk/deactivate
tx: row lock ─> Applies ─> Run: self? 403 ; guardLastSuperuser (advisory lock + count others) ;
update is_activated=false, tokens_valid_after=now+1s
```
### Recommended Project Structure
```
modules/cabana/
├── backend_screens.go # pseudo-plugin (party.Plugin + HasAdminControllers, AdminAssets,
│ # HasPermissions, HasSettings), embed of backendscreens/
├── backend_users_admin.go # summercms.cabana.users controller: hooks, actions, providers
├── backend_roles_admin.go # summercms.cabana.roles controller
├── backend_privileges.go # actsAsSuperuser, guardLastSuperuser, grant subset/ceiling helpers
├── action_extensions.go # compileActionExtensions (D-14 merge)
├── backendscreens/ # embedded AdminFS (go:embed)
│ ├── controllers/users/{config_list,config_form,config_filter}.yaml
│ ├── controllers/roles/{config_list,config_form}.yaml
│ └── models/{user,userrole}/{columns,fields}.yaml
modules/pact/capabilities.go # + AdminActionExtension, HasAdminActionExtensions
modules/phrasebook/backend/lang/{en,pl}/lang.yaml # + user.*, user.role.*, permissions/settings keys
```
### Pattern 1: Conditional internal pseudo-plugin in `Activate`
**What:** append the pseudo-plugin only when real controllers exist, and give it the compiled registry after compilation.
**Why:** `Activate` has `if len(items) == 0 { return nil, nil }` [VERIFIED: modules/cabana/http.go:79-81]; `examples/hello` has no admin controllers and no JWT secret, and `modules/surf/admin_prefix_test.go:91-99,194` asserts "no admin controllers" → not mounted [VERIFIED: read this session].
**Rules it must satisfy:** controller IDs must start with the plugin ID (`id != p.ID() && !strings.HasPrefix(id, p.ID()+".")` is an error [VERIFIED: registry.go:33-36]); the route derives the ID from three segments `vendor.plugin.controller` [VERIFIED: http.go:1102]; the reserved first segments are `map[string]bool{"api": true, "assets": true, "login": true, "settings": true, "account": true}` [VERIFIED: registry.go:46] — `summercms` is free. A plugin with controllers needs `pact.AdminAssets` (`"cabana: plugin %s has admin controllers but no AdminFS"` [VERIFIED: registry.go:80-83]). `party.Plugin` is `ID() string; Requires() []string; Register(app *backpack.App) error; Boot(app *backpack.App) error` [VERIFIED: modules/party/registry.go:16-21].
**Recommended IDs:** plugin `summercms.cabana`, controllers `summercms.cabana.users` and `summercms.cabana.roles` (SPA route `/backend/summercms/cabana/users`). The migration history id `summercms.cabana` stays in lagoon untouched (`migrator(gdb, "summercms.cabana", BackendAdminMigrations)` [VERIFIED: modules/lagoon/migrations.go:75]); no migration is needed.
```go
// Sketch (identifiers are the planner's call). Source: modules/cabana/http.go:74-130 shape.
items, err := collectControllers(plugins)
if err != nil { return nil, err }
if len(items) == 0 { return nil, nil } // unchanged: no admin, no secret check
screens := &backendScreens{app: app} // party.Plugin, ID "summercms.cabana"
plugins = append(append([]party.Plugin(nil), plugins...), screens)
if items, err = collectControllers(plugins); err != nil { return nil, err }
// ... compileFieldTypes, compileRegistryWithTypes(items, fieldTypes) ...
// if err := compileActionExtensions(reg, plugins); err != nil { return nil, err } // before compileContributions
// ... compileContributions(reg, plugins), compileTokenScopes ...
screens.reg = reg // controllers read the catalog per request
```
### Pattern 2: The D-14 seam as a boot-time merge into existing record actions
**What:** a plugin-level capability (like `HasAdminControllers`, not a controller capability) returning contributions keyed by controller ID. The merge appends each contributed name to `cc.Form.recordActions` and the action to `cc.RecordActions`; after that `offeredRecordActions`, `recordActionOf`, `RecordAction` (row lock, Applies re-check, `TxFromContext`), the diary log line and `validatePermissions` all work unchanged.
**Why the merge is required:** both lookups iterate only declared names — `for _, name := range cc.Form.recordActions` [VERIFIED: crud.go:557] and `for _, entry := range cc.Form.recordActions { declared = declared || entry == name }` [VERIFIED: actions.go:242-247]. The compiled list is `recordActions []string` [VERIFIED: schema_types.go:159], unexported, so it is not in any payload.
**Boot checks to reuse:** an actions-only preview rule already exists: `recordActions needs a preview block (record actions are offered on the preview screen)` [VERIFIED: extension.go:91-93]; names must be identifiers, not `builtinToolbarActions = map[string]bool{"create": true, "delete": true}` [VERIFIED: extension.go:47], unique, with `Run` and a non-empty `Label` [VERIFIED: extension.go:94-102, 401-427]. Add: unknown controller ID → error; target without a form or preview block → error; duplicate against the owner's names and other contributions → error. `compileContributions` then validates each action's `Permissions` against the catalog because it iterates `controller.RecordActions` [VERIFIED: registry.go:342-346] — so the merge must run **before** `compileContributions`.
**Shape (additive, discretion on names):**
```go
// modules/pact/capabilities.go — uses the existing AdminRecordAction type verbatim:
// type AdminRecordAction struct { Name, Label, Confirm string; Permissions []string; Danger bool;
// Applies func(ctx context.Context, record any) (bool, error); Run func(ctx context.Context,
// in AdminRecordActionInput) (AdminRecordActionResult, error) } [VERIFIED: capabilities.go:343-354]
// AdminActionExtension adds actions to an admin controller the plugin may not own.
type AdminActionExtension struct {
Controller string // controller ID, vendor.plugin.controller
RecordActions []AdminRecordAction // offered on the controller's preview screen after its own
}
// HasAdminActionExtensions is implemented by plugins (not controllers).
type HasAdminActionExtensions interface {
AdminActionExtensions() []AdminActionExtension
}
```
A struct (not a bare slice) lets `BulkActions` be added later as a field without breaking implementers. **Recommendation: record actions only in this phase.** Bulk contributions would also need entries in the list schema's `BulkActions` view (label/confirm built from YAML today), which is not free.
**Protection is inherited:** `protect()` checks the target controller's `RequiredPermissions` before any action, then `allowAction` checks the action's own `Permissions` [VERIFIED: http.go:1101-1110, actions.go:256-262]. A contributed action on the Administrators screen therefore needs `backend.manage_users` **and** its own code.
**19.6 D-15 can use it as is**, with two conditions: the target `golem15.user.users` already has a preview block (`preview:` + `recordActions: [activate, unban, unsuspend]` [VERIFIED: fonoteka.go/plugins/golem15/user/controllers/users/config_form.yaml:14-17]), and the contributed name must not collide with a name the user plugin itself registers. 19.6-01-PLAN already plans an in-plugin `reset_two_factor` slot (the user plugin registers the action, mfa publishes `classes.TwoFactorAdmin`) [VERIFIED: 19.6-01-PLAN.md:49,270]; if that ships, mfa must not also contribute `reset_two_factor`. Nothing in 19.6 has to change.
**19.7 caveat:** `AdminRecordActionInput` carries only `RecordID uint64` and `Record any` [VERIFIED: capabilities.go:360-363] — no payload. "Remove link" fits; "set a link for editor onboarding" (19.7 D-07) needs a chosen frontend user, which a record action cannot carry. See Open Question 5.
### Pattern 3: Administrators controller over `*cabana.BackendUser`
**Model requirements:** the generic CRUD needs `pact.AdminRecordSource`, a model implementing `lagoon.HasFillable` and `Rules() map[string]string` (`newWritableModel` [VERIFIED: crud.go:907-927]); `scope` filters need `pact.FilterScope` **on the model** (`filterProvider` asserts `src.NewRecord().(pact.FilterScope)` [VERIFIED: filter_schema.go:311-322]); `FilterOptions` may be on the controller [VERIFIED: filter_schema.go:301-309]. Recommend adding `Fillable()`, `Rules()`, `FilterScopes()`, `FilterScope()` to `BackendUser` (additive; `BackendUser` is used by sm-journal-plugin as a relation target and importer type, none of which consults these methods) and the same `Fillable`/`Rules` to `BackendUserRole`.
**Protected keys** (never filled, never projected, never bound): `"id", "created_at", "updated_at", "deleted_at", "owner_id", "user_id", "collection_id", "organisation_id", "organization_id", "scope_id", "role_id", "permissions", "is_superuser", "is_system", "is_activated", "password"` [VERIFIED: crud.go:1407-1417]. Consequences:
- `role` is a `type: relation` belongsTo with `FieldRelationContract{Field: "role", Kind: "belongsTo", NewRelated: ..., ForeignKey: "role_id", LabelColumn: "name", WritableForeignKey: true}` — `WritableForeignKey` is the documented opt-in for a protected foreign key [VERIFIED: relation_field.go:41-43, 247], the same as sm-user-plugin's `organisation` [VERIFIED: sm-grzybyfunkcjonalne-app/plugins/golem15/user/controllers/users_admin_controller.go:348-357].
- `password`, `password_confirmation` and `is_superuser` are listed in `FormVirtualFields`; allowed virtual types are `"password", "text", "textarea", "number", "checkbox", "switch", "dropdown"` [VERIFIED: extension.go:158-161]. A `type: password` field must be listed (`checkVirtualFields` [VERIFIED: extension.go:170-175]). Virtual values reach hooks through `cabana.VirtualFieldsFromContext`, only for keys present in the body ("a missing key means 'not submitted'") [VERIFIED: tx_context.go:46-70, crud.go:1269-1288] — so D-05's "sending `is_superuser` → 403" is detectable even when the field is hidden.
- `permissions` uses `type: permissioneditor` (`mode: radio` for users, `mode: checkbox` for roles); it is lifted separately and never filled [VERIFIED: field_permission.go:177-211].
- `is_activated` only changes through record/bulk actions (D-10).
**Save order (the timing every guard depends on)** [VERIFIED: crud.go:771-848]: `lagoon.Fill` → `Validate` (merged `FormRules`) → `formBeforeUpdate` (line 800) → `applyPermissionValues` (809) → `checkRelationScope` → `checkRelationLocks` (819) → `assignBelongsTo` (822) → `tx.Unscoped().Save(target)` (829) → `formAfterUpdate` (848). In `FormBeforeUpdate` the model still holds the **stored** `RoleID`, `IsSuperuser`, `IsActivated` and `Permissions` (protected keys are never filled); the new `role_id` appears only after `assignBelongsTo`.
**Hook plan (the guards):**
| Where | Check | Answer |
|-------|-------|--------|
| `ListExtendQuery`, `FormExtendQuery` | `db.Unscoped()` (trashed admins listable and restorable, as sm-user-plugin does [VERIFIED: users_admin_controller.go:84-97]); non-superuser → `Where("backend_users.is_superuser = ?", false)` (qualified: list search uses correlated subqueries next to unqualified scope predicates [VERIFIED: query.go:485-490]) | hidden superuser by id = 404 through `loadRecord`'s not-found [VERIFIED: crud.go:946-953] |
| `FormBeforeCreate` | virtual `is_superuser` present and not `actsAsSuperuser` → 403; set `IsSuperuser`; set `IsActivated = true`, `ActivatedAt = now` (as `admin:create` does [VERIFIED: commands.go:106-115]); hash password; login/email checks | `*cabana.ForbiddenError` (403) / `*cabana.ValidationError` (422) |
| `FormBeforeUpdate` | snapshot prior `RoleID`; `is_superuser` present: non-superuser → 403, self and changed → 403, demotion of an active superuser → `guardLastSuperuser`; password: hash, bump `TokensValidAfter` (self: see OQ 2); login/email uniqueness | 403 / 422 |
| `AdminSetPermissionValues` (users) | self and `next` ≠ stored → 403; store JSON | 403 |
| `AdminRelationLocks(ctx, "role")` | non-superuser: lock every role whose merged grants ⊄ editor's | framework 403 on change [VERIFIED: relation_field.go:55-67] |
| `FormAfterUpdate` | self and `RoleID` ≠ snapshot → 403 (rolls the tx back) | 403 |
| `FormBeforeDelete` | self → 403; active superuser → `guardLastSuperuser` | 403 |
| `FormAfterDelete` | bump `tokens_valid_after` on the (now trashed) row through `tx.Unscoped()` | — |
| record + bulk `activate` / `deactivate` / `restore` | deactivate: self → 403, active superuser → `guardLastSuperuser`, bump; activate: set `activated_at`; restore: `deleted_at = NULL` | 403 |
`deleteRecord` calls `FormBeforeDelete` once per row inside the bulk delete transaction [VERIFIED: crud.go:366-373, 1127-1143], so one refused row rolls back the whole selection, and the per-row "count the others" check sees earlier rows' writes in the same tx.
**The `is_superuser` projection gap.** Protected keys are skipped in `projectRecord` [VERIFIED: crud.go:1354-1378] and virtual fields are "never bound, filled or projected" [VERIFIED: contracts.go:133-135]. The SPA sends every non-readOnly field on save (`editablePayload` [VERIFIED: admin/src/components/form/formState.ts:53-77]). An unprojected switch therefore shows "off" for a superuser and is sent as `false` when the form is saved → silent demotion. Fix: an **unexported** in-package interface that `projectFullRecord` consults (only cabana's own controllers can implement an unexported method), adding `is_superuser` to `data`. The same mechanism (another unexported interface consulted in `formSchema`/`listSchema`) removes the `is_superuser` field and filter for non-superusers (D-03); today `formSchema` only drops widget fields per request [VERIFIED: http.go:767-785]. No `pact` surface, no payload change for other controllers.
**Prior-state snapshot.** Use an unexported field on `BackendUser` (for example `adminPrior *uint` plus a set flag). GORM parses only exported fields (`if fieldStruct := modelType.Field(i); ast.IsExported(fieldStruct.Name)` [VERIFIED: gorm.io/gorm@v1.31.2/schema/schema.go:220-221]) and cabana's `flattenModelFields` skips `f.PkgPath != ""` [VERIFIED: model_fields.go:54-56]. The same pointer is passed to `formBeforeUpdate` and `formAfterUpdate` [VERIFIED: crud.go:800, 848].
### Pattern 4: `guardLastSuperuser` under an advisory transaction lock
**What:** one function every reducing path calls before it writes: take `pg_advisory_xact_lock(<constant bigint>)`, then count `is_superuser AND is_activated AND deleted_at IS NULL AND id <> target` through the write tx (`cabana.TxFromContext`); refuse when the count is 0 and the target is currently an active superuser.
**Why it is correct:** every path takes its target row lock first (`loadRecord`/`lockScoped`, `FOR UPDATE` [VERIFIED: crud.go:946, 1058-1060]) and then the advisory lock; nothing takes a row lock after the advisory lock (the count is a plain `SELECT`), so there is no wait cycle. Under READ COMMITTED, the second transaction's count runs after the first commits (it waited on the advisory lock), so it sees the first demotion. Re-entrant within one tx, so a bulk action calling it per row is fine.
**Scope:** only paths that reduce the set (demote, deactivate, delete). Create/restore/activate raise it. `admin:create`/`admin:reset-password` never clear the flag [VERIFIED: commands.go:71-168], so they stay unchanged (SC-1); 19.8 already reshaped those commands' output (commit `9d06a78`), so do not touch `commands.go`.
### Pattern 5: Ceilings (D-07)
- **Effective grants of a role** = `parseGrants(role.Permissions)` ∪ `reg.rolePermissions(role.Code)` — the same merge as `FindByID` [VERIFIED: auth.go:39-67, registry.go:449-460].
- **Editor's grants** = `principal.PermissionGrants` (already merged per request by `FindByID`).
- **Subset test:** for each role code `c` without a wildcard, `granted(editor, c)` [VERIFIED: contracts.go:214-248]. **Do not** pass a wildcard role grant (`acme.*`) to `granted`/`Allows` — a required code ending in `*` means "any matching grant", not "all" [VERIFIED: contracts.go:216-226]. Treat a wildcard role grant as covered only when the editor holds the same or a broader wildcard (conservative; see A3).
- **Options:** `AdminPermissionOptions` returns the catalog (`reg.permissions`, a map — add an ordered slice in `compilePermissionCatalog` for stable display) with `Locked: !actsAsSuperuser(p) && !granted(p.PermissionGrants, code)`. The framework then keeps stored non-offered codes, refuses a changed locked value with `ForbiddenError` on the field (`permissionLockedKey = "backend::lang.permissioneditor.locked"` [VERIFIED: field_permission.go:24, 303-307]).
- `actsAsSuperuser(p)` = `p != nil && p.IsSuperuser && (p.Scope == nil || p.Scope.Full)` — a superuser's scoped personal token never grants superuser-only powers. `Scope` is `*ScopeLimit{Full bool; Scopes [][]string}` [VERIFIED: modules/bouncer/context.go:16-35].
### Pattern 6: Roles controller (D-16, D-17)
- Fields: `name`, `code` (`preset: {type: slug, field: name}` — `preset` is a supported field key [VERIFIED: form_schema.go:47]), `description` (`textarea`), `permissions` (`permissioneditor`, `mode: checkbox`). Roles have no `deleted_at` (`BackendUserRole` has no `DeletedAt` [VERIFIED: contracts.go:17-26]) → delete is a hard delete.
- `code` is fillable (not protected), so `Fill` has already written it before `FormBeforeUpdate`: re-read the stored row through a fresh session on the tx (sm-user-plugin's `guardCredentials` pattern [VERIFIED: users_admin_controller.go:458-470]) and refuse a code change on a system role (`is_system` or `reg.roleGrants[code]` non-empty).
- `FormBeforeDelete`: system role → refuse; count admins with `role_id` **unscoped** (soft-deleted admins still reference the row through `role_id INTEGER REFERENCES backend_user_roles(id)` with no `ON DELETE` [VERIFIED: backend_admin_migrations.go:50]) → `ValidationError` naming the count.
- Rules: `name` required + `unique:backend_user_roles` (DB has `name TEXT NOT NULL UNIQUE` [VERIFIED: backend_admin_migrations.go:29]); `code` unique too (DB has only an index, `code TEXT,` [VERIFIED: :30]; `roleIDByCode` fails on an ambiguous code [VERIFIED: commands.go:192-207]).
### Anti-Patterns to Avoid
- **Hand-written routes for the screens.** Every route exists generically; new routes would need swag annotations, OpenAPI regeneration and SPA work.
- **Using `RelationExtendOptionsQuery` for the role ceiling** (see Alternatives).
- **Checking `role_id` in `FormBeforeUpdate`.** It still holds the stored value.
- **`Preload("Role")` in `FormExtendQuery`.** GORM saves a non-zero belongs-to association and resets the foreign key from it (`setupReferences` → `ref.ForeignKey.Set(..., pv)`; a zero association is skipped) [VERIFIED: gorm@v1.31.2/callbacks/associations.go:24-29, 92-93], so a preloaded old `Role` would overwrite the new `role_id` on `Save`.
- **Trusting lagoon's `unique` rule for login/email.** It adds `deleted_at IS NULL` and is case-sensitive [VERIFIED: modules/lagoon/validate.go:359-381]; the DB has `login TEXT NOT NULL UNIQUE`, `email TEXT NOT NULL UNIQUE` [VERIFIED: backend_admin_migrations.go:41-42] plus `lower(email)` unique, all including soft-deleted rows.
## Don't Hand-Roll
| Problem | Don't Build | Use Instead | Why |
|---------|-------------|-------------|-----|
| Permission ceiling on save | own diffing of submitted permissions | `PermissionEditorProvider` + `Locked` | `applyPermissionValues` already keeps unoffered codes, rejects unknown codes and 403s locked changes |
| Role ceiling | own check on `role_id` | `RelationLockProvider` | Enforced before any row write, shows locked options |
| Password hashing / rehash | custom bcrypt | `bouncer.HashPassword(adminBcryptCost(app), pw)` | Same cost and 72-byte limit as login and `admin:create` |
| Session revocation | blacklist entries | `tokens_valid_after` | JWT guard compares `iat` per request [VERIFIED: bouncer/jwt.go:160-173] |
| Record/bulk action plumbing | new endpoints | `HasAdminRecordActions` / `HasAdminBulkActions` | Row locks, Applies re-check, CSRF, diary log already done |
| Settings placement | navigation item | `pact.SettingsItem{Controller: ...}` link entries | Listed only when the principal can open the controller [VERIFIED: navigation.go:87-93] |
**Key insight:** the only new mechanisms are the D-14 merge, two unexported projection/schema hooks, and the last-superuser lock. Everything else is configuration of existing seams.
## Common Pitfalls
### Pitfall 1: Silent superuser demotion from an unprojected switch
**What goes wrong:** a superuser edits another superuser's name; the form sends `is_superuser: false`; the target is demoted.
**Why:** protected and virtual fields are not projected; the SPA sends every field.
**How to avoid:** project `is_superuser` through the unexported hook; test that GET returns it and that saving an untouched form keeps it.
### Pitfall 2: Self role change slips through `FormBeforeUpdate`
**Why:** `assignBelongsTo` runs after the hook. **Avoid:** snapshot + `FormAfterUpdate` check, with a test that sends a new `role` for the editor's own record and asserts 403 and an unchanged row.
### Pitfall 3: "No controllers means no admin" broken
**What goes wrong:** `examples/hello` and every admin-less app fail boot with `cabana: admin.jwt.secret is empty`; `surf` prefix tests fail. **Avoid:** conditional pseudo-plugin; run `cd examples/hello && go test ./...` and `go test ./modules/surf`.
### Pitfall 4: Soft-deleted rows still own their login and email
**What goes wrong:** creating `editor@example.com` after deleting an admin with that email is a 500 (unique violation). **Avoid:** an unscoped, case-insensitive check mirroring `admin:create` (`login IN (?, ?) OR lower(email) IN (?, ?)`, excluding the record's own id [VERIFIED: commands.go:96]); answer 422 on the field and mention that a deleted administrator holds the value (restore it).
### Pitfall 5: Role delete hits the FK
**Why:** soft-deleted admins still reference the role. **Avoid:** unscoped count in `FormBeforeDelete`.
### Pitfall 6: Deadlock instead of a refusal under concurrency
**Avoid:** advisory lock, not `FOR UPDATE` on the superuser set (Pattern 4).
### Pitfall 7: Personal API tokens survive a password reset and revive on restore
**What:** `tokens_valid_after` cuts JWTs only; personal tokens are cut by `FindByID` refusing inactive/deleted users (`token_guard.go:100-104`) and are explicitly untouched by `admin:reset-password` ("Personal API tokens are not touched" [VERIFIED: commands.go:47-50]). So D-12's "personal tokens then stop" holds for deactivate and delete, not for a password set; and restore/activate (which don't bump) bring the admin's personal tokens back. See Open Question 6.
### Pitfall 8: Existing tests and consumers see two new settings entries for superusers
**What:** a superuser's `/settings` gains "Administrators" and "Roles". Non-superuser lists are unchanged (D-04: no default role grant), so e.g. the fonoteka developer-settings assertion (`reflect.DeepEqual(settings, []string{"fonoteka"})`) still holds [VERIFIED: fonoteka.go/plugins/golem15/fonoteka/admin_metadata_test.go:156-158]. Grep sibling repos for superuser-settings assertions before the framework tag.
### Pitfall 9: bcrypt input limit
`HashPassword` refuses input over 72 bytes; My Account checks `len(password) > passwordMaxBytes` (72) itself [VERIFIED: account.go:46-47, 335-336]. Add the same byte check for the admin form's password, with `min:4` as in Winter (`'password' => 'required:create|min:4|confirmed'` [VERIFIED: examples/golem15-wintercms-starter/modules/backend/models/User.php:30]).
### Pitfall 10: Missing lang keys
`backend::lang.user.*` does not exist yet: the backend namespace has only `auth, nav, page, list, form, relation, settings, extension, datepicker, money, taglist, secret, fileupload, messages, permissioneditor, api_tokens, myaccount, richeditor, repeater, reorder` [VERIFIED: modules/phrasebook/backend/lang/en/lang.yaml top-level keys]. `validateMessageKeys` fails boot on a missing `messages.*` phrase key [VERIFIED: messages.go:296-315]. Port the strings from Winter's `modules/backend/lang/en/lang.php` and `pl/lang.php` (`'user' => [ 'name' => 'Administrator', 'menu_label' => 'Administrators', ...` [VERIFIED: examples/golem15-wintercms-starter/modules/backend/lang/en/lang.php:133-137]).
## Code Examples
### The last-superuser guard (skeleton)
```go
// Source: pattern from this research; TxFromContext from modules/cabana/tx_context.go:27-33.
// The lock key is any fixed bigint constant (planner picks it).
func guardLastSuperuser(ctx context.Context, target *BackendUser) error {
if !target.IsSuperuser || !target.IsActivated || target.DeletedAt.Valid {
return nil // the write does not shrink the active superuser set
}
tx, ok := TxFromContext(ctx)
if !ok {
return errors.New("cabana: last-superuser check needs the write transaction")
}
db := tx.Session(&gorm.Session{NewDB: true}).WithContext(ctx)
if err := db.Exec("SELECT pg_advisory_xact_lock(?)", superuserLockKey).Error; err != nil {
return err
}
var others int64
if err := db.Model(&BackendUser{}).
Where("is_superuser AND is_activated AND id <> ?", target.ID).
Count(&others).Error; err != nil { // default scope adds deleted_at IS NULL
return err
}
if others == 0 {
return &ForbiddenError{Message: "backend::lang.user.last_superuser"} // new phrase key
}
return nil
}
```
### Revocation like `admin:reset-password`
```go
// Source: modules/cabana/commands.go:157-162 (verbatim values):
// cutoff := time.Now().UTC().Add(time.Second)
// ... Updates(map[string]any{"password": hash, "tokens_valid_after": cutoff})
cutoff := time.Now().UTC().Add(time.Second)
user.TokensValidAfter = &cutoff // in FormBeforeUpdate; Save writes it
```
### A contribution from another plugin (what 19.7 writes)
```go
// Neutral example; Permissions must be a code some activated plugin registers.
func (p *Plugin) AdminActionExtensions() []pact.AdminActionExtension {
return []pact.AdminActionExtension{{
Controller: "summercms.cabana.users",
RecordActions: []pact.AdminRecordAction{{
Name: "unlink_profile", Label: "acme.profiles::lang.unlink", Danger: true,
Permissions: []string{"acme.profiles.manage_links"},
Applies: func(ctx context.Context, record any) (bool, error) { /* pure read */ return true, nil },
Run: func(ctx context.Context, in pact.AdminRecordActionInput) (pact.AdminRecordActionResult, error) {
tx, ok := cabana.TxFromContext(ctx) // the action's row-locked transaction
_ = tx; _ = ok; _ = in.RecordID // backend_users.id
return pact.AdminRecordActionResult{Message: "acme.profiles::lang.unlinked"}, nil
},
}},
}}
}
```
### Settings link entries (D-02)
```go
// pact.SettingsItem fields: Code, Label, Description, Category, Icon, Model, Order,
// Keywords, Permissions, Controller, Form, NewModel [VERIFIED: modules/pact/capabilities.go:570-587].
// A link entry declares Controller and no Model/Form/NewModel [VERIFIED: settings.go:57-65].
pact.SettingsItem{Code: "administrators", Controller: "summercms.cabana.users",
Permissions: []string{"backend.manage_users"}, Label: "backend::lang.user.menu_label",
Category: "<new system category phrase key>"}
```
`Code` must be an identifier and unique across all settings [VERIFIED: registry.go:287-292] — check that no installed plugin already uses `administrators`/`roles` (none found in the sibling repos grepped this session).
### Filters available to the users list
Supported filter types are only `switch` (boolean `column`), `daterange` (time `column`) and `scope` (`scope`, `modelClass`, `nameFrom` required; `FilterScope` on the model and `FilterOptions` to serve choices); Winter's `conditions` key is refused [VERIFIED: filter_schema.go:112-114, 170-190, 218-248]. So: `is_superuser` → `switch`; `role` → `scope`; trashed → `scope` with two options (only deleted / without deleted); last login → `daterange`.
## State of the Art
| Old Approach | Current Approach | When Changed | Impact |
|--------------|------------------|--------------|--------|
| Admins only through `admin:create` / `admin:reset-password` | SPA screens + same commands | this phase | docs `users-and-permissions.md` "Managing administrators" and the My Account paragraph ("role, superuser flag, permissions and activation stay with the operator commands below" [VERIFIED: docs/backend/users-and-permissions.md:138]) must change |
| Winter: no last-superuser rule; self-edit redirects to My Account | D-08/D-09 server rules | this phase | Deliberate tightening (CONTEXT specifics) |
| Winter: system role permissions entirely disabled and code-defined; developer inherits orphaned permissions [VERIFIED: modules/backend/models/UserRole.php:38-63, 91-99] | Stored extras editable on top of code grants (D-16); cabana merges stored + code grants and has no orphan rule [VERIFIED: auth.go:54-66] | — | No behaviour change to `FindByID` in this phase |
## Assumptions Log
| # | Claim | Section | Risk if Wrong |
|---|-------|---------|---------------|
| A1 | Adding `Fillable`/`Rules`/`FilterScopes`/`FilterScope` methods and an unexported field to the exported `cabana.BackendUser` is non-breaking for consumers (none found using unkeyed literals or `==` on it) | Pattern 3 | A consumer that compares `BackendUser` values or embeds it in a model with its own `Rules` could change behaviour; mitigated by grepping sibling repos before tagging |
| A2 | `pg_advisory_xact_lock` ordering after row locks is deadlock-free for every path listed | Pattern 4 | A future path that takes a row lock after the advisory lock could deadlock; the concurrency test catches regressions |
| A3 | Conservative wildcard rule for the role ceiling (a role's `x.*` grant is covered only by an equal or broader editor wildcard) is acceptable | Pattern 5 | Could lock roles a non-superuser should be able to assign; superusers unaffected |
| A4 | Recommended IDs `summercms.cabana.users` / `summercms.cabana.roles` (URLs `/backend/summercms/cabana/...`) are acceptable instead of Winter's `backend/users` | Pattern 1 | Purely cosmetic; IDs become stored in nothing except settings codes and links |
| A5 | Record actions only (no bulk/toolbar) in the D-14 seam this phase | Pattern 2 | 19.7 may want a bulk "unlink"; the struct shape keeps it additive |
| A6 | The SPA renders the new controllers with no `admin/` change | Summary | If a gap shows up in UAT (for example the users-count column), it means an admin/ change and a dist rebuild |
## Open Questions (RESOLVED)
1. **[RESOLVED: D-19 (no outranking).]** **Can a non-superuser manage an admin who outranks them?** (security, not in CONTEXT)
- What we know: D-07 stops granting more than you hold, but a non-superuser with `backend.manage_users` can still set the password of, deactivate, or delete a non-superuser admin whose role grants more than their own. Setting the password is an account takeover, which escalates privilege. sm-user-plugin guards its privileged groups the same way (`guardPrivilegedMember` [VERIFIED: users_admin_controller.go:192-209]).
- Recommendation: add an "outranks" refusal. When the target's effective grants are not a subset of the editor's, a non-superuser gets a 403 on update, delete and actions, using the Pattern 5 helper. This needs user confirmation.
2. **[RESOLVED: D-20 (own password refused, points to My Account).]** **Changing your own password on the Administrators screen.** D-08 doesn't list `password`. My Account requires the current password and mints a new session; this screen would do neither. Recommendation: refuse a password change on your own record here and point to My Account. This needs confirmation.
3. **[RESOLVED: D-21 chose (b), real `trigger` support in plan 02.]** **D-06 "disabled when `is_superuser` is on".** cabana's fields.yaml has no `trigger` key: the accepted keys are listed in `formFieldKeys` and include no `trigger` [VERIFIED: form_schema.go:38-50]. Options:
- (a) server-side only: superusers bypass `Allows`, so their per-user permissions are inert, and a comment on the field says so;
- (b) add `trigger` support, which changes `admin/` (dist rebuild, possibly the OpenAPI schema).
- Recommendation: (a), recorded as a deviation.
4. **[RESOLVED: D-21 chose (b), per-record locks in plan 02.]** **D-16 "code-declared grants shown as locked" per role.** `PermissionOption.Locked` is per request, not per record: `AdminPermissionOptions(ctx, field)` gets no record, and the form schema is fetched without an id [VERIFIED: field_permission.go:61-65, http.go:753-759]. Options:
- (a) show the merged values. A save that drops a code-declared grant is refused with a 403 using the locked phrase, or the code-declared grants are stripped from the stored JSON, since they are merged at runtime anyway;
- (b) an optional record-level locks provider whose codes travel in record meta, which changes `admin/` and the OpenAPI schema.
- Recommendation: (a). This needs confirmation, because it softens "shown as locked".
5. **[RESOLVED: deferred to 19.7; noted in the seam docs.]** **19.7 "set a link" needs input.** Record actions carry no payload [VERIFIED: capabilities.go:360-363].
- Option: `AdminRecordActionInput` could later gain an optional `Payload json.RawMessage`, mirroring `AdminActionInput.Payload` [VERIFIED: capabilities.go:245-251]. That is additive, but it needs SPA input UI.
- Recommendation: keep it out of 19.6.1, design it in 19.7, and note it in the seam docs.
6. **[RESOLVED: D-12 as amended 2026-10-22: a password set keeps personal tokens; deactivate and delete stop them (plan 01 Decisions).]** **Personal API tokens on password set and on restore** (Pitfall 7).
- Options: revoke the target's personal tokens on deactivate and delete, or accept today's semantics and document them.
- Recommendation: document them, matching `admin:reset-password`. Revoking tokens would widen the phase.
7. **[RESOLVED: omitted in this phase (plan 01).]** **Users count on the Roles list.** Winter shows `users_count`. cabana columns have no count/`valueFrom` key: `columnDocument` holds `Label`, `Searchable`, `Sortable`, `Type`, `Relation`, `Select`, `Invisible`, `Currency`, `CurrencyFrom` [VERIFIED: list_schema.go:62-73]. Recommendation: omit it in this phase.
## Environment Availability
| Dependency | Required By | Available | Version | Fallback |
|------------|------------|-----------|---------|----------|
| Go toolchain | build, tests | ✓ | go1.27.2 | — |
| Docker daemon | Postgres testcontainers (privilege, concurrency tests) | ✓ | Server 29.9.1 | `sudo -u golem` if the socket is denied (CLAUDE.md) |
| Node / npm | only if `admin/` changes | ✓ | v22.23.3 / 12.2.1 | not needed by the recommended design |
**Missing dependencies with no fallback:** none.
## Validation Architecture
### Test Framework
| Property | Value |
|----------|-------|
| Framework | Go `testing`; testcontainers-go Postgres (`postgres:16-alpine`) through `adminGorm(t)`, which skips under `-short` [VERIFIED: auth_test.go:321-325] |
| Config file | none (package helpers `adminGorm`, `adminHandler`, `insertAdmin` in `modules/cabana/auth_test.go`, package `cabana_test`) |
| Quick run command | `go test -short ./modules/cabana -run 'TestActionExtension\|TestBackendScreens\|TestRoleCeiling'` |
| Full suite command | `go vet ./... && go test ./... && (cd examples/hello && go test ./...) && go test ./cmd/summer -run TestDocsTree && go run ./cmd/summer docs:build --check` |
### Phase Requirements → Test Map
(No requirement IDs are mapped; rows key on decisions.)
| Decision | Behavior | Type | Automated Command | File Exists? |
|----------|----------|------|-------------------|-------------|
| Activation | no real controllers → `Activate` nil, hello boots without secret; with controllers → pseudo-plugin controllers, permissions, 2 settings links | unit | `go test -short ./modules/cabana -run TestBackendScreensActivation`; `go test ./modules/surf -run TestAdminPrefix`; `cd examples/hello && go test ./...` | ❌ Wave 0 (surf/hello exist) |
| D-14 | boot errors: unknown controller, no preview, duplicate vs owner, duplicate between contributors, reserved name, nil Run, empty label, unknown permission | unit | `go test -short ./modules/cabana -run TestActionExtensionBoot` | ❌ |
| D-14 | offered only with permission + Applies, after owner's actions; run path row-locks, re-checks Applies (409), runs in tx, requires target controller permission | Postgres | `go test ./modules/cabana -run TestActionExtensionRun` | ❌ |
| D-03 | non-superuser: list excludes superusers, GET superuser id 404, schema lacks `is_superuser` field and filter; superuser sees them | Postgres + unit | `go test ./modules/cabana -run TestAdministratorsSuperuserHidden` | ❌ |
| D-05 | non-superuser sending `is_superuser` (true or false) on create/update → 403, row unchanged; superuser can grant | Postgres | `go test ./modules/cabana -run TestAdministratorsSuperuserFlag` | ❌ |
| Pitfall 1 | GET projects `is_superuser`; saving an untouched superuser form keeps the flag | Postgres | `-run TestAdministratorsSuperuserProjected` | ❌ |
| D-06/D-07 | per-user radio: locked codes for non-held perms, changed locked → 403, stored kept; Roles checkbox same | Postgres | `-run TestAdministratorsPermissionCeiling\|TestRolesPermissionCeiling` | ❌ |
| D-07 | role options locked above ceiling; assigning locked role → 403; unchanged locked current role saves fine | Postgres | `-run TestAdministratorsRoleCeiling` | ❌ |
| D-07 | subset helper incl. wildcard cases | unit (package cabana) | `go test -short ./modules/cabana -run TestRoleCeiling` | ❌ |
| D-08 | self: change role (FormAfterUpdate path) / is_superuser / permissions / deactivate / delete → 403; profile fields save | Postgres | `-run TestAdministratorsSelfEdit` | ❌ |
| D-09 | last active superuser: demote, deactivate (record + bulk), delete (form + bulk) → refused, rollback; with two superusers bulk-deleting both → refused | Postgres | `-run TestAdministratorsLastSuperuser` | ❌ |
| D-09 | concurrent: two goroutines demote two different superusers; a third tx holds the advisory lock until `pg_locks` shows 2 waiters, then releases; exactly one succeeds, ≥1 active superuser remains, no deadlock error | Postgres | `-run TestAdministratorsLastSuperuserConcurrent` | ❌ |
| D-10 | activate/deactivate/restore record + bulk; soft delete; trashed filter; row state `deleted` | Postgres | `-run TestAdministratorsLifecycle` | ❌ |
| D-11 | create needs password + confirmation; update blank = unchanged; bcrypt verifies; >72 bytes 422; duplicate login/email incl. deleted and case variants 422 | Postgres | `-run TestAdministratorsPasswordAndIdentity` | ❌ |
| D-12 | after deactivate / delete / password set for another admin, the target's old JWT → 401 next request; restore/activate don't bump | Postgres (HTTP) | `-run TestAdministratorsRevocation` | ❌ |
| D-16/D-17 | system role code change refused; delete with holders (incl. soft-deleted) → 422 naming count; system role delete refused | Postgres | `-run TestRolesLifecycle` | ❌ |
| SC-1 | `admin:*` commands and `/auth/me`, `/account` payloads unchanged | existing | `go test ./modules/cabana -run 'TestAdmin\|TestAccount\|TestMe'` | ✅ |
### Sampling Rate
- **Per task commit:** `go vet ./modules/cabana ./modules/pact && go test -short ./modules/cabana ./modules/pact ./modules/surf`
- **Per wave merge:** `go test ./modules/cabana ./modules/surf` (Docker) + `cd examples/hello && go test ./...`
- **Phase gate:** full suite green; optionally `scripts/check-phase19.6.1.sh` wrapping the above, as earlier phases do (`scripts/check-phase19.5.sh`).
### Wave 0 Gaps
- [ ] `modules/cabana/backend_screens_test.go`: activation and schema (`-short`)
- [ ] `modules/cabana/action_extensions_test.go`: D-14 boot and run
- [ ] `modules/cabana/backend_privileges_internal_test.go`: subset/ceiling helpers (`package cabana`)
- [ ] `modules/cabana/administrators_postgres_test.go`: D-03..D-12 HTTP-level, through `adminHandler` and `insertAdmin`. Each test seeds its own uniquely named admins and cleans them up, because the container is shared by the package.
- [ ] `modules/cabana/roles_postgres_test.go`: D-16/D-17
- [ ] `insertAdmin` has no superuser flag; add a helper that sets `is_superuser`.
## Security Domain
### Applicable ASVS Categories
| ASVS Category | Applies | Standard Control |
|---------------|---------|-----------------|
| V2 Authentication | yes (password set) | bcrypt via `bouncer.HashPassword`, min 4 / max 72 bytes, confirmation |
| V3 Session Management | yes | `tokens_valid_after` cutoff, `FindByID` refusal of inactive/deleted |
| V4 Access Control | yes (core of the phase) | `RequiredPermissions`, action `Permissions`, D-05..D-09 hooks, `RelationLockProvider`, `Locked` options, query scopes |
| V5 Input Validation | yes | lagoon rules + `FormRules`; protected fill keys; JSON-int permission values |
| V6 Cryptography | no new use | — |
### Known Threat Patterns for this stack
| Pattern | STRIDE | Standard Mitigation |
|---------|--------|---------------------|
| Mass assignment of `is_superuser`/`role_id`/`permissions` | Elevation | `protectedFillKey`; only the controller sets them, after the checks |
| Hidden-field bypass (a non-superuser posts `is_superuser`) | Elevation | Presence check on the virtual value → 403 (D-05) |
| Self-escalation through your own role or permissions | Elevation | D-08 checks (snapshot in `FormAfterUpdate`; `AdminSetPermissionValues`) |
| Granting above your own permissions | Elevation | D-07 locks |
| Account takeover of a higher-privileged admin by password reset | Elevation | Open Question 1 "outranks" refusal |
| Removing the last superuser (lockout) | Denial of service | `guardLastSuperuser` under an advisory lock |
| Superuser enumeration by id | Information disclosure | `FormExtendQuery` scope → 404 |
| A scoped personal token used for superuser-only operations | Elevation | `actsAsSuperuser` requires an unscoped or full scope |
| A contributed action skipping the screen's permission | Elevation | `protect()` checks the controller's permissions before the action's own |
## Sources
### Primary (HIGH confidence, read this session)
- `modules/cabana/{contracts,crud,extension,registry,http,actions,auth,commands,field_permission,relation_field,filter_schema,form_schema,settings,navigation,tx_context,model_fields,account,token_guard,messages,query,list_schema,schema_types}.go`
- `modules/pact/capabilities.go`, `modules/party/registry.go`, `modules/bouncer/context.go`, `modules/lagoon/{backend_admin_migrations,validate,fill}.go`, `modules/surf/{router,admin_prefix_test}.go`
- `gorm.io/gorm@v1.31.2` `schema/schema.go`, `callbacks/associations.go` (module cache)
- `admin/src/components/form/{formState.ts,fields/PermissionEditorField.vue,fields/SwitchField.vue}`, `admin/src/views/PreviewView.vue`
- sm-user-plugin Users screen: `fonoteka.go/plugins/golem15/user/controllers/` and `sm-grzybyfunkcjonalne-app/plugins/golem15/user/{controllers,classes/privileged.go}`
- WinterCMS: `examples/golem15-wintercms-starter/modules/backend/{controllers/Users.php,controllers/UserRoles.php,models/UserRole.php,models/User.php,formwidgets/PermissionEditor.php,models/user/*.yaml,models/userrole/*.yaml,controllers/users/*.yaml,lang/en/lang.php}`
- Planning: 19.6.1 CONTEXT, 19.7 CONTEXT/RESEARCH, 19.6 CONTEXT and 19.6-01-PLAN, 19.8-04-PLAN, `docs/backend/users-and-permissions.md`, `docs/backend/admin-controllers.md`
### Secondary (MEDIUM)
- PostgreSQL advisory lock functions: postgresql.org/docs/current/functions-admin.html (cited from documentation knowledge; not fetched this session)
## Metadata
**Confidence breakdown:**
- Standard stack: HIGH. No new dependencies; versions were read from go.mod and the local toolchain.
- Architecture: HIGH for the mechanics, including save order, protected keys, lookups and activation. MEDIUM for the recommended shapes (the seam names, the unexported hooks and the snapshot field), which are discretion items.
- Pitfalls: HIGH. Each pitfall traces to a cited line.
**Research date:** 2026-10-21
**Valid until:** 2026-11-04. cabana is under active change (19.6, 19.7 and 19.8 are in flight), so re-check `crud.go` save order and `Activate` before executing.

View File

@@ -0,0 +1,550 @@
---
phase: "19.6.1"
slug: "backend-administrators-screen"
status: approved
reviewed_at: "2026-10-21"
shadcn_initialized: false
preset: none
created: "2026-10-21"
---
# Phase 19.6.1 — UI Design Contract
> Visual and interaction contract for frontend phases. Generated by msd-ui-researcher, verified by msd-ui-checker.
This phase adds **no new screen design**. The Administrators and Roles screens are YAML-declared cabana controllers (`columns.yaml`, `fields.yaml`, `config_*.yaml`) rendered by the existing generic views in `admin/` (`ListView.vue`, `FormView.vue`, `PreviewView.vue`, `SettingsIndexView.vue`). The contract therefore fixes four things:
1. **What the YAML declares**: columns, filters, form tabs and field order, preview, record and bulk actions, settings entries. These are layout decisions made with existing components.
2. **Two narrow SPA additions (D-21)**:
- a field `trigger` (`action: disable|hide`, `field`, `condition: checked|unchecked`) evaluated in `FormView`/`PreviewView`;
- per-record locked permission values rendered by `PermissionEditorField.vue`.
3. **All copy** (en + pl) for both screens, the settings entries, the permission catalog, record/bulk actions, the preview status hint and every privilege refusal (D-05, D-07, D-08, D-09, D-17, D-19, D-20).
4. **Where each refusal surfaces** in the existing UI (banner, field line, toast), so the server picks an error shape the SPA already displays.
The SPA has a complete design system: Direction C v2 (`.planning/phases/10-admin-vue-spa/design/README.md`), implemented as Tailwind 4 `@theme` tokens in `admin/src/styles/main.css` on Reka UI primitives. **This contract adds no tokens, colours, fonts, radii, shadows or icons outside the existing set.** Every class string below is copied from an existing component (`FormField.vue`, `PermissionEditorField.vue`, `RelationField.vue`, `RecordActions.vue`, `FormErrorBanner.vue`, `RowStateBadges.vue`, `main.css` callout kit).
Sources: 19.6.1-CONTEXT.md D-01..D-22 (locked); 19.6.1-RESEARCH.md (Patterns 1-6, Pitfalls, Open Questions 1-4 now resolved by D-19..D-21); approved house conventions from the 12.1, 12.2 and 16.3 UI-SPECs; WinterCMS `modules/backend/models/{user,userrole}/*.yaml` and `lang/{en,pl}/lang.php` (`user.*` block) for structure and wording; the codebase scan listed per section.
---
## Design System
| Property | Value |
|----------|-------|
| Tool | none (no shadcn; Vue 3 SPA with its own Direction C system. The shadcn gate does not apply to a Vue codebase with an established system, the same ruling as Phases 10.1, 12.1, 12.2, 16.3) |
| Preset | not applicable |
| Component library | Reka UI 2.9.10 (headless primitives) plus the local components in `admin/src/components/` |
| Icon library | `@lucide/vue` 1.17.0 (named imports only). Settings tiles use the registry names `users` and `shield-check` through `admin/src/app/icons.ts`; the trigger note uses `Info` 14px; locks use the existing `Lock` 14px |
| Font | DM Sans 400/600/700 (self-hosted `@fontsource/dm-sans`); no DM Mono in this phase |
---
## Component Inventory
Enumerated by `node --input-type=module -e "import * as r from 'reka-ui'; console.log(Object.keys(r).length)"` (run in `admin/`) — 478 exports — reka-ui@2.9.10 — 2026-10-21.
Local components enumerated by `find admin/src/components -name '*.vue' | wc -l` — 59 components — summercms-admin@unversioned (private workspace package with no `version` field in `admin/package.json`; pinned by repo commit 053c643) — 2026-10-21.
Icon presence verified by `node --input-type=module -e "import * as l from '@lucide/vue'; for (const n of ['Lock','Info','CircleAlert','Users','ShieldCheck']) console.log(n, n in l)"` (run in `admin/`) against @lucide/vue@1.17.0 on 2026-10-21: all exported. `users` and `shield-check` are already mapped in `admin/src/app/icons.ts`.
This table is a **non-exhaustive** list of known-good components for this phase, not a closed allowlist. Checking for another Reka export or local component is the expected path.
| Component | Import path | Notes |
|-----------|-------------|-------|
| ListView | `admin/src/views/ListView.vue` | Both lists. Toolbar create/delete, search, filters, bulk actions menu, row-state badges. No change. |
| FormView | `admin/src/views/FormView.vue` | Both forms. Gains trigger evaluation (Contract C6) and threads record permission locks (C7). |
| PreviewView | `admin/src/views/PreviewView.vue` | Administrators preview with the `hint` header partial and `RecordActions`. Gains `hide` trigger evaluation only (C6). |
| FormField | `admin/src/components/form/FormField.vue` | Gains the trigger note line (C6). Label, comment, error and notice markup unchanged. |
| PermissionEditorField | `admin/src/components/form/fields/PermissionEditorField.vue` | Radio mode (per-user overrides) and checkbox mode (role grants). Gains record locks (C7). |
| SwitchField | `admin/src/components/form/fields/SwitchField.vue` | The `is_superuser` toggle card. No change. |
| RelationField | `admin/src/components/form/fields/RelationField.vue` | The `role` belongsTo dropdown. Its existing locked-option handling (`Lock` icon, `form.locked_note`) renders the D-07 role ceiling. No change. |
| PasswordField | `admin/src/components/form/fields/PasswordField.vue` | `password` and `password_confirmation`. No change. |
| RecordActions | `admin/src/components/form/RecordActions.vue` | Preview footer actions, owner's and contributed (D-14, D-15). No change. |
| FormErrorBanner | `admin/src/components/form/FormErrorBanner.vue` | 403 forbidden banner and 422 banner (C8). No change. |
| RowStateBadges | `admin/src/components/list/RowStateBadges.vue` | `deleted` and `disabled` states on the Administrators list. No change. |
| PartialHost + `.summer-callout` kit | `admin/src/components/partial/PartialHost.vue`, `main.css` | Preview status hint (`_hint.htm`, html/template, server-rendered). No change. |
| ConfirmDialog + `useConfirm` | `admin/src/components/ui/ConfirmDialog.vue`, `confirm.ts` | Record, bulk and delete confirmations. No change. |
| `showToast` | `admin/src/state/useToasts.ts` | Action results and delete refusals. No change. |
| `editablePayload`, `permissionValues` | `admin/src/components/form/formState.ts`, `control.ts` | Payload rules change for triggered fields (C6) and record-locked codes (C7). |
New SPA code is limited to: trigger evaluation helpers (suggested `admin/src/components/form/trigger.ts`), the note line in `FormField.vue`, the record-lock prop/inject path into `PermissionEditorField.vue`, and the regenerated `admin/src/api/schema.d.ts`. No new `.vue` file is required.
---
## Spacing Scale
Declared values for new layout in this phase (multiples of 4):
| Token | Value | Tailwind | Usage |
|-------|-------|----------|-------|
| xs | 4px | `gap-1` | not used by new markup |
| sm | 8px | `gap-2` | Trigger note: icon-to-text gap (copied from the `RelationField` locked note `gap-2`) |
| md | 16px | `px-4` | Permission editor row padding (existing, unchanged) |
| lg | 24px | `gap-x-6` | Form grid column gap (existing `FormGrid`, unchanged) |
| xl | 32px | `px-8` | Sticky footer padding (existing, unchanged) |
| 2xl | 48px | — | not used |
| 3xl | 64px | — | not used |
Exceptions (inherited from Direction C and already approved as locked in 16.3 on 2026-10-11; re-used verbatim, none introduced): label-to-control gap 6px (`gap-1.5` in `FormField`); form row gap 22px (`gap-y-[22px]` in `FormGrid`); card padding 28px (`p-7`); page stack 20px (`gap-5`); footer button gap 10px (`gap-2.5`); toggle card / callout vertical padding 14px (`py-3.5`); callout padding 14x18 (`.summer-callout`, `px-[18px] py-3.5` banner); permission row 12px vertical padding (`py-3`) and 56px minimum row height.
The trigger note line sits inside `FormField`'s existing `flex-col gap-1.5` stack, so it adds no new spacing value.
---
## Typography
New markup uses exactly three styles; everything else is rendered by existing components unchanged.
| Role | Size | Weight | Line Height | Used by |
|------|------|--------|-------------|---------|
| Body | 14px | 400 | 1.5 | Field values, callout text, toasts, banner text (all existing) |
| Label | 14px | 600 | 1.5 | Field labels, permission row labels, callout titles, button text (all existing) |
| Meta | 13px | 400 | 1.5 | **New:** the trigger note line and the record-lock note line. Existing: field comments, error lines, the locked note |
| Page heading (inherited) | 24px | 700 | 1.2 (`tracking-[-0.02em]`) | Record title in `FormView`/`PreviewView` header, unchanged |
Weights: new content uses 400 and 600 only. 700 appears only in the inherited page heading (locked system-level exception, as in 16.3). No 12px text is added (the existing 12px row-state badge is reused unchanged).
---
## Color
All colours are existing CSS variables (light / dark from `main.css`). Hard-coded hex values are forbidden in new markup.
| Role | Value (light / dark) | Usage |
|------|----------------------|-------|
| Dominant (60%) | `bg` #f4f6f9 / #111726, `surface` #ffffff / #182033 | Page, form card, list table, permission editor rows |
| Secondary (30%) | `subtle` #f3f5f8 / #1f283d, `border` #e6e9ef / #29334b, `border-strong` #d2d8e2 / #3a4661, `muted` #566175 / #a9b3c6 | Permission section headers, segmented control track, read-only boxes, the trigger note and lock note text, `Lock`/`Info` icons, the "Deleted" row badge outline |
| Accent (10%) | `primary` #22304d / #fcd34d; `sel` #fdf3cf / #3a3622; `ring` | See reserved list |
| Destructive | `danger` #c62828 / #f58a8a, `danger-soft` #fdf0f0 / #3a1d24 | Form footer Delete (danger outline), delete confirm buttons, forbidden and 422 banners, field error lines, the "deleted" preview callout, danger toasts |
| Semantic OK | `ok-bg` / `ok-text` | Existing "Allow" segment checked state and the "Yes" switch pill on preview; unchanged |
Accent reserved for (exhaustive for this phase):
1. `primary` fill: the single primary button per screen that already exists: footer **Save** on forms, **Edit administrator** on the preview, **New administrator** / **New role** list toolbar create. No new primary button is added.
2. `primary` track: the `is_superuser` switch when on (existing `SwitchField`), and checked role-grant checkboxes in the permission editor (existing).
3. `sel` tint: the warning variant of the preview status callout (deactivated account; your own account), via `.summer-callout--warning`.
4. `ring` (3px): focus-visible on every focusable element (base style), never removed.
Accent is never used for: record action buttons (outline), the trigger note, lock icons, row-state badges, settings tiles, or links.
---
## Copywriting Contract
All copy lives in `modules/phrasebook/backend/lang/{en,pl}/lang.yaml` (`backend::lang.*`). New groups: `user:` (with a nested `role:` block, mirroring Winter's `backend::lang.user.*` and `backend::lang.user.role.*` names so ported plugins and docs keep the same keys) and `permissions:`. Additions to existing groups: `form.trigger_*`, `permissioneditor.locked_*`, `settings.category_system`. Placeholders use `:name` / `:count` / `:field`. Plural texts are CLDR maps (`one`/`other` in en; `one`/`few`/`many`/`other` in pl), never pipe strings. Polish is the design language; English values follow the same meaning.
Validation messages that already exist (`required`, `min.string`, `max.string`, `confirmed`, `unique`) are reused for password and name rules; no new copy for them.
### Primary CTAs and key states
| Element | Key | EN | PL |
|---------|-----|----|----|
| Controller record name (`config_form.yaml` `name`) | `user.name` / `user.role.name` | Administrator / Role | Administrator / Rola |
| Administrators list title | `user.list_title` | Administrators | Administratorzy |
| Primary CTA, Administrators (toolbar create; create-form title) | `user.new` | New administrator | Nowy administrator |
| Preview primary (Edit) | `user.edit` | Edit administrator | Edytuj administratora |
| Preview subtitle | `user.preview` | Administrator details | Szczegóły administratora |
| Roles list title | `user.role.list_title` | Roles | Role |
| Primary CTA, Roles (toolbar create; create-form title) | `user.role.new` | New role | Nowa rola |
| Role edit title | `user.role.edit` | Edit role | Edytuj rolę |
| Save, Save and close, Cancel, Delete | existing `form.*` | unchanged | unchanged |
| Empty state, Administrators (`noRecordsMessage`) | `user.list_empty` | No administrators in this view. Clear the search and filters, or create an administrator. | Brak administratorów w tym widoku. Wyczyść wyszukiwanie i filtry lub utwórz administratora. |
| Empty state, Roles (`noRecordsMessage`) | `user.role.list_empty` | No roles in this view. Clear the search, or create a role. | Brak ról w tym widoku. Wyczyść wyszukiwanie lub utwórz rolę. |
| Empty search (both lists) | existing `messages.list.empty_search` / `empty_search_hint` | unchanged | unchanged |
| Error state, list or form load failed | existing `list.load_failed`, `form.load_failed` | unchanged (also the D-03 404 for a hidden superuser) | unchanged |
| Error state, save failed (non-422/403) | existing `form.error_generic` | unchanged | unchanged |
| Saved toast | existing `messages.form.saved` | unchanged | unchanged |
### Settings entries and permission catalog (D-01, D-02)
| Element | Key | EN | PL |
|---------|-----|----|----|
| Settings category | `settings.category_system` | System | System |
| Administrators tile label | `user.menu_label` | Administrators | Administratorzy |
| Administrators tile description | `user.menu_description` | Manage administrator accounts, their roles and permissions. | Zarządzaj kontami administratorów, ich rolami i uprawnieniami. |
| Roles tile label | `user.role.menu_label` | Roles | Role |
| Roles tile description | `user.role.menu_description` | Define roles and the permissions they grant. | Określ role i uprawnienia, które nadają. |
| Permission tab (catalog section) | `permissions.tab_system` | System | System |
| `backend.manage_users` label | `permissions.manage_users` | Manage administrators | Zarządzanie administratorami |
| `backend.manage_users` comment | `permissions.manage_users_comment` | Create, edit, deactivate and delete administrator accounts. | Tworzenie, edycja, dezaktywacja i usuwanie kont administratorów. |
| `backend.manage_roles` label | `permissions.manage_roles` | Manage roles | Zarządzanie rolami |
| `backend.manage_roles` comment | `permissions.manage_roles_comment` | Create and edit roles and the permissions they grant. | Tworzenie i edycja ról oraz uprawnień, które nadają. |
### Administrators: list
| Element | Key | EN | PL |
|---------|-----|----|----|
| Column / filter: login | `user.login` | Login | Login |
| Column: first name | `user.first_name` | First name | Imię |
| Column: last name | `user.last_name` | Last name | Nazwisko |
| Column: email | `user.email` | Email | E-mail |
| Column / filter: role | `user.role_field` | Role | Rola |
| Column / filter: last sign-in | `user.last_login` | Last sign-in | Ostatnie logowanie |
| Column / filter: superuser (superusers only) | `user.superuser` | Superuser | Superużytkownik |
| Column (invisible): created | `user.created_at` | Created | Utworzono |
| Filter: active | `user.activated` | Active | Aktywny |
| Filter: deleted (scope) | `user.filter_deleted` | Deleted | Usunięci |
| Filter option | `user.filter_deleted_hide` | Hide deleted | Ukryj usuniętych |
| Filter option | `user.filter_deleted_only` | Only deleted | Tylko usunięci |
| Row state `deleted` | `user.state_deleted` | Deleted | Usunięty |
| Row state `disabled` | `user.state_deactivated` | Deactivated | Dezaktywowany |
| Delete selected confirm (danger) | `user.delete_selected_confirm` | one: "Delete :count administrator? They are signed out at once. Deleted accounts can be restored." other: "Delete :count administrators? They are signed out at once. Deleted accounts can be restored." | one: "Usunąć :count administratora? Zostanie od razu wylogowany. Usunięte konta można przywrócić." few/many: "Usunąć :count administratorów? Zostaną od razu wylogowani. Usunięte konta można przywrócić." other: "Usunąć :count administratora? Zostaną od razu wylogowani. Usunięte konta można przywrócić." |
| Deleted toast | existing `messages.list.deleted` | unchanged | unchanged |
### Administrators: form
| Element | Key | EN | PL |
|---------|-----|----|----|
| Tab 1 | `user.account` | Account | Konto |
| Tab 2 | `user.permissions` | Permissions | Uprawnienia |
| Password label | `user.password` | Password | Hasło |
| Password comment | `user.password_comment` | At least 4 characters. When editing, leave blank to keep the current password. | Co najmniej 4 znaki. Przy edycji zostaw puste, aby zachować obecne hasło. |
| Password confirmation label | `user.password_confirmation` | Confirm password | Potwierdź hasło |
| Role comment | `user.role_comment` | The role sets this administrator's permissions. Single permissions can be adjusted on the Permissions tab. | Rola określa uprawnienia administratora. Pojedyncze uprawnienia można zmienić w zakładce Uprawnienia. |
| Role empty option | `user.role_none` | No role | Brak roli |
| Superuser comment | `user.superuser_comment` | Grants every permission in the admin, including managing other administrators. Permission overrides below are not used while this is on. | Daje wszystkie uprawnienia w panelu, w tym zarządzanie innymi administratorami. Gdy jest włączone, nadpisania uprawnień poniżej nie są używane. |
| Permission overrides label | `user.permission_overrides` | Permission overrides | Nadpisania uprawnień |
| Permission overrides comment | `user.permission_overrides_comment` | Inherit follows the role. Allow and Deny apply to this administrator only. | Dziedzicz oznacza ustawienie z roli. Zezwól i Odmów dotyczą tylko tego administratora. |
| Delete confirm (form footer, danger) | `user.delete_confirm` | Delete this administrator? They are signed out at once. The account can be restored from the list. | Usunąć tego administratora? Zostanie od razu wylogowany. Konto można przywrócić z listy. |
### Administrators: preview, record actions and bulk actions (D-10, D-15)
| Element | Key | EN | PL |
|---------|-----|----|----|
| Preview-only field: last sign-in | `user.last_login` | Last sign-in | Ostatnie logowanie |
| Record + bulk action: Activate | `user.activate` | Activate | Aktywuj |
| Activate confirm (record) | `user.activate_confirm` | Activate this administrator? They can sign in again, and their existing API tokens work again. | Aktywować tego administratora? Będzie mógł się zalogować, a jego dotychczasowe tokeny API znów zadziałają. |
| Activate confirm (bulk) | `user.activate_selected_confirm` | Activate the selected administrators? They can sign in again, and their existing API tokens work again. | Aktywować zaznaczonych administratorów? Będą mogli się zalogować, a ich dotychczasowe tokeny API znów zadziałają. |
| Activate success (record / bulk) | `user.activated` / `user.activated_selected` | Administrator activated / Selected administrators activated | Aktywowano administratora / Aktywowano zaznaczonych administratorów |
| Record + bulk action: Deactivate | `user.deactivate` | Deactivate | Dezaktywuj |
| Deactivate confirm (record) | `user.deactivate_confirm` | Deactivate this administrator? They are signed out at once and cannot sign in until activated again. | Dezaktywować tego administratora? Zostanie od razu wylogowany i nie zaloguje się, dopóki konto nie zostanie ponownie aktywowane. |
| Deactivate confirm (bulk) | `user.deactivate_selected_confirm` | Deactivate the selected administrators? They are signed out at once and cannot sign in until activated again. | Dezaktywować zaznaczonych administratorów? Zostaną od razu wylogowani i nie zalogują się, dopóki ich konta nie zostaną ponownie aktywowane. |
| Deactivate success (record / bulk) | `user.deactivated` / `user.deactivated_selected` | Administrator deactivated / Selected administrators deactivated | Dezaktywowano administratora / Dezaktywowano zaznaczonych administratorów |
| Record + bulk action: Restore | `user.restore` | Restore | Przywróć |
| Restore confirm (record) | `user.restore_confirm` | Restore this administrator? If the account is active, they can sign in again and their existing API tokens work again. | Przywrócić tego administratora? Jeśli konto jest aktywne, będzie mógł się zalogować, a jego dotychczasowe tokeny API znów zadziałają. |
| Restore confirm (bulk) | `user.restore_selected_confirm` | Restore the selected administrators? Active accounts can sign in again, and their existing API tokens work again. | Przywrócić zaznaczonych administratorów? Aktywne konta będą mogły się zalogować, a ich dotychczasowe tokeny API znów zadziałają. |
| Restore success (record / bulk) | `user.restored` / `user.restored_selected` | Administrator restored / Selected administrators restored | Przywrócono administratora / Przywrócono zaznaczonych administratorów |
| Hint: deleted (danger) title / text | `user.trashed_hint_title` / `user.trashed_hint_desc` | This account has been deleted / It cannot be used to sign in. Use Restore to bring it back. | To konto zostało usunięte / Nie można się na nie zalogować. Użyj „Przywróć”, aby je odzyskać. |
| Hint: deactivated (warning) title / text | `user.deactivated_hint_title` / `user.deactivated_hint_desc` | This account is deactivated / It cannot be used to sign in. Use Activate to allow sign-in again. | To konto jest dezaktywowane / Nie można się na nie zalogować. Użyj „Aktywuj”, aby ponownie zezwolić na logowanie. |
| Hint: own account (warning) title / text | `user.self_hint_title` / `user.self_hint_desc` | This is your account / You can change your name, login and email here. Your role, permissions, superuser access and activation can only be changed by another administrator. Change your password in My account. | To Twoje konto / Tutaj zmienisz imię, nazwisko, login i e-mail. Rolę, uprawnienia, dostęp superużytkownika i aktywację może zmienić tylko inny administrator. Hasło zmień w sekcji Moje konto. |
Contributed record actions (D-14) bring their own label, confirm and success copy from the contributing plugin's phrasebook; this phase adds none.
### Roles: list and form (D-16, D-17)
| Element | Key | EN | PL |
|---------|-----|----|----|
| Name label / comment | `user.role.name_field` / `user.role.name_comment` | Name / Shown in the role picker on the administrator form. | Nazwa / Wyświetlana na liście ról w formularzu administratora. |
| Code label / comment | `user.role.code_field` / `user.role.code_comment` | Code / A unique identifier used by plugins and the API. The code of a system role cannot be changed. | Kod / Unikalny identyfikator używany przez wtyczki i API. Kodu roli systemowej nie można zmienić. |
| Description label | `user.role.description_field` | Description | Opis |
| Column: system role | `user.role.is_system` | System role | Rola systemowa |
| Permissions tab | `user.permissions` | Permissions | Uprawnienia |
| Granted permissions label / comment | `user.role.permissions_field` / `user.role.permissions_comment` | Granted permissions / Every administrator with this role gets the checked permissions. | Nadane uprawnienia / Każdy administrator z tą rolą otrzymuje zaznaczone uprawnienia. |
| Delete confirm (form footer, danger) | `user.role.delete_confirm` | Delete this role? This cannot be undone. A role that is still assigned to administrators cannot be deleted. | Usunąć tę rolę? Tej operacji nie można cofnąć. Roli przypisanej do administratorów nie można usunąć. |
| Delete selected confirm (danger) | `user.role.delete_selected_confirm` | one: "Delete :count role? This cannot be undone." other: "Delete :count roles? This cannot be undone." | one: "Usunąć :count rolę? Tej operacji nie można cofnąć." few: "Usunąć :count role? Tej operacji nie można cofnąć." many: "Usunąć :count ról? Tej operacji nie można cofnąć." other: "Usunąć :count roli? Tej operacji nie można cofnąć." |
### Framework additions (D-21)
| Element | Key | EN | PL |
|---------|-----|----|----|
| Trigger note, `condition: checked` | `form.trigger_disabled_on` | Not editable while “:field” is on. | Nie można edytować, gdy „:field” jest włączone. |
| Trigger note, `condition: unchecked` | `form.trigger_disabled_off` | Not editable while “:field” is off. | Nie można edytować, gdy „:field” jest wyłączone. |
| Record lock note: grant declared in code (Roles) | `permissioneditor.locked_by_code` | Granted to this role by a plugin. It cannot be removed here. | Nadane tej roli przez wtyczkę. Nie można go tu odebrać. |
| Record lock note: your own record (Administrators) | `permissioneditor.locked_self` | You cannot change your own permissions. | Nie możesz zmieniać własnych uprawnień. |
### Privilege refusals (server copy)
All of these are phrase keys the server puts in a `ForbiddenError` (`Message` and/or `Details`) or a `ValidationError` detail. Contract C8 maps where each one renders.
| Decision | Key | EN | PL |
|----------|-----|----|----|
| D-05 | `user.superuser_forbidden` | Only a superuser can grant, remove or change superuser access. | Tylko superużytkownik może nadawać, odbierać lub zmieniać dostęp superużytkownika. |
| D-07 role ceiling (`RelationLock.Message`) | `user.role_ceiling` | You can only assign roles whose permissions you have yourself. | Możesz przypisywać tylko role, których uprawnienia sam posiadasz. |
| D-07 permission ceiling | existing `permissioneditor.locked` | You cannot change this permission. | Nie możesz zmienić tego uprawnienia. |
| D-08 own privileged fields | `user.self_privileged` | You cannot change your own role, permissions, superuser access or activation. Ask another administrator. | Nie możesz zmieniać własnej roli, uprawnień, dostępu superużytkownika ani aktywacji. Poproś innego administratora. |
| D-08 delete yourself | `user.self_delete` | You cannot delete your own account. | Nie możesz usunąć własnego konta. |
| D-08 deactivate yourself | `user.self_deactivate` | You cannot deactivate your own account. | Nie możesz dezaktywować własnego konta. |
| D-09 | `user.last_superuser` | This is the last active superuser, so it cannot be deactivated, deleted or lose superuser access. Make another administrator a superuser first. | To ostatni aktywny superużytkownik, więc nie można go dezaktywować, usunąć ani odebrać mu dostępu superużytkownika. Najpierw nadaj ten dostęp innemu administratorowi. |
| D-19 | `user.outranked` | This administrator has permissions you do not have, so you cannot change, deactivate or delete their account. | Ten administrator ma uprawnienia, których nie masz, więc nie możesz zmieniać, dezaktywować ani usuwać jego konta. |
| D-20 | `user.own_password` | Change your own password in My account, where your current password is checked. | Własne hasło zmień w sekcji Moje konto, która sprawdza obecne hasło. |
| D-17 role has admins | `user.role.delete_has_users` | one: "The role “:name” is assigned to :count administrator. Give them another role first. Deleted accounts count too." other: "The role “:name” is assigned to :count administrators. Give them another role first. Deleted accounts count too." | one: "Rola „:name” jest przypisana do :count administratora. Najpierw przydziel mu inną rolę. Liczą się też usunięte konta." few/many: "Rola „:name” jest przypisana do :count administratorów. Najpierw przydziel im inną rolę. Liczą się też usunięte konta." other: "Rola „:name” jest przypisana do :count administratora. Najpierw przydziel inną rolę. Liczą się też usunięte konta." |
| D-17 system role | `user.role.delete_system` | System roles cannot be deleted. | Ról systemowych nie można usunąć. |
| D-16 system role code | `user.role.code_locked` | The code of a system role cannot be changed. | Kodu roli systemowej nie można zmienić. |
| Role code taken | `user.role.code_taken` | This code is already used by another role. | Ten kod jest już używany przez inną rolę. |
| Login taken | `user.login_taken` | This login is already used by another administrator. | Ten login jest już używany przez innego administratora. |
| Login held by a deleted admin | `user.login_taken_deleted` | This login belongs to a deleted administrator. Restore that account instead of creating a new one. | Ten login należy do usuniętego administratora. Przywróć to konto zamiast tworzyć nowe. |
| Email taken | `user.email_taken` | This email is already used by another administrator. | Ten adres e-mail jest już używany przez innego administratora. |
| Email held by a deleted admin | `user.email_taken_deleted` | This email belongs to a deleted administrator. Restore that account instead of creating a new one. | Ten adres e-mail należy do usuniętego administratora. Przywróć to konto zamiast tworzyć nowe. |
### Destructive actions
| Action | Confirmation approach | Copy |
|--------|----------------------|------|
| Delete an administrator (form footer) | Existing FormView `confirm.ask({ danger: true })` with `messages.deleteConfirm` | `user.delete_confirm`; confirm button existing `form.delete` |
| Delete selected administrators (list toolbar) | Existing ListView danger confirm with the plural `messages.deleteConfirm` | `user.delete_selected_confirm` |
| Deactivate (record and bulk) | Existing confirm, not `danger` (reversible, `AdminRecordAction.Danger` false) | `user.deactivate_confirm`, `user.deactivate_selected_confirm`; the confirm button label is the action label "Deactivate" |
| Delete a role (form footer) | Existing danger confirm | `user.role.delete_confirm` |
| Delete selected roles | Existing danger confirm | `user.role.delete_selected_confirm` |
| Demote a superuser (switch off and Save) | No dialog: the save itself is the confirmation, as for every form field. The server refuses with D-09 when it would remove the last active superuser | — |
---
## Component Contracts
### C1. Settings placement (D-02)
- Two `pact.SettingsItem` link entries in category `backend::lang.settings.category_system`, rendered by the existing `SettingsIndexView` tiles (no SPA change):
- `administrators`: label `user.menu_label`, description `user.menu_description`, icon `users`, `Controller: summercms.cabana.users`, `Permissions: [backend.manage_users]`, order 100;
- `roles`: label `user.role.menu_label`, description `user.role.menu_description`, icon `shield-check`, `Controller: summercms.cabana.roles`, `Permissions: [backend.manage_roles]`, order 110.
- Administrators comes before Roles in the group. A tile appears only when the principal can open its controller (existing filter), so an admin holding only `backend.manage_roles` sees only Roles.
- No top-level navigation item and no section flyout entry.
### C2. Administrators list
`config_list.yaml` (shape copied from sm-user-plugin's Users list):
- `title: user.list_title`, `recordUrl` → the record's **preview** (D-15), `recordsPerPage: 20`, `showCheckboxes: true`, `showSetup: true`, `noRecordsMessage: user.list_empty`.
- Toolbar: `buttons: [create, delete]` (create label `user.new`; delete is the soft delete), search prompt `list.search_prompt`.
- `bulkActions: [activate, deactivate, restore]`.
- Messages: `create: user.new`, `empty: user.list_empty`, `deleteConfirm: user.delete_selected_confirm`, `deleted: messages.list.deleted`, `rowStateDeleted: user.state_deleted`, `rowStateDisabled: user.state_deactivated`. No `rowStateNegative`.
- Row states (`ListRowStates`): `deleted` when `deleted_at` is set; `disabled` when not activated. A deleted and deactivated row shows both badges, in `RowStateBadges`' fixed order.
Columns, in this order:
| Column | Type | Visible | Searchable / sortable | Notes |
|--------|------|---------|----------------------|-------|
| `login` | text | yes | searchable, sortable | |
| `first_name` | text | yes | searchable, sortable | |
| `last_name` | text | yes | searchable, sortable | |
| `email` | text | yes | searchable, sortable | |
| `role` | relation `role`, select `name` | yes | sortable | Empty cell shows the existing `list.empty_value` dash |
| `last_login` | datetime | yes | sortable | Never signed in shows the dash |
| `is_superuser` | switch | yes, **superusers only** | — | Removed from the schema for non-superusers by the same per-request hook that removes the field and filter (D-03) |
| `created_at` | datetime | invisible | sortable | Available through list setup |
Filters (supported types only: `switch`, `daterange`, `scope`):
| Scope | Type | Label | Notes |
|-------|------|-------|-------|
| `role` | scope (`modelClass` role, `nameFrom: name`) | `user.role_field` | |
| `is_superuser` | switch, column `is_superuser` | `user.superuser` | Superusers only (D-03) |
| `activated` | switch, column `is_activated` | `user.activated` | |
| `last_login` | daterange, column `last_login` | `user.last_login` | |
| `trashed` | scope with two options | `user.filter_deleted` | Options `user.filter_deleted_hide` and `user.filter_deleted_only`. With no option chosen the list shows every administrator, deleted ones marked with the "Deleted" badge (the list query is unscoped, as in sm-user-plugin) |
### C3. Administrators form
`config_form.yaml`: `name: user.name` (en "Administrator", pl "Administrator"), `preview: { headerPartial: hint }`, `recordActions: [activate, deactivate, restore]`, redirects copied from sm-user-plugin (create → the new record's preview; update `redirect` → stays on update; `redirectClose` → preview), messages `create: user.new`, `update: messages.form.update`, `saved: messages.form.saved`, `deleteConfirm: user.delete_confirm`, `deleted: messages.form.deleted`, `edit: user.edit`, `preview: user.preview`.
Record title (header h1, breadcrumb, `:name` in messages): "First Last" when either name is set, otherwise the login.
Fields, by tab (two-column `FormGrid`; `left`/`right` pair on one row):
**Tab `user.account` (the default tab)**
| Field | Type | Span | Context | Required | Notes |
|-------|------|------|---------|----------|-------|
| `login` | text | left | create, update, preview | yes | |
| `email` | text | right | create, update, preview | yes | |
| `first_name` | text | left | all | no | |
| `last_name` | text | right | all | no | |
| `password` | password (virtual) | left | create, update | create only | Comment `user.password_comment`. Blank on update means unchanged (existing `editablePayload` rule) |
| `password_confirmation` | password (virtual) | right | create, update | create only | |
| `role` | relation belongsTo, `nameFrom: name`, `emptyOption: user.role_none` | full | all | no | Comment `user.role_comment`. Roles above the editor's ceiling render locked through `RelationLockProvider` (D-07), using `RelationField`'s existing lock icon and `form.locked_note` |
| `last_login` | datepicker (datetime) | left | **preview only** | — | Read-only display on the preview screen; never in a save body |
**Tab `user.permissions`**
| Field | Type | Span | Context | Notes |
|-------|------|------|---------|-------|
| `is_superuser` | switch (virtual, projected) | full | create, update, preview | Label `user.superuser`, comment `user.superuser_comment`. Present only for superusers (D-03). Renders as the existing toggle card |
| `permissions` | permissioneditor, `mode: radio` | full | create, update, preview | Label `user.permission_overrides`, comment `user.permission_overrides_comment`. `trigger: { action: disable, field: is_superuser, condition: checked }` (C6, D-06). Options locked above the editor's grants (D-07, existing `Locked`); every option record-locked on the editor's own record (C7, D-08) |
- The tab order is fixed: Account, then Permissions. The Permissions tab is always present (a non-superuser still sees the overrides editor without the switch).
- The `FormView` footer is unchanged: Delete (danger outline, update only), Cancel (ghost), Save and close (outline), Save (primary).
- No client-side validation is added; 422 errors render under fields and in the existing banner, and the first invalid field's tab is activated (existing).
### C4. Administrators preview (D-15)
- Rendered by `PreviewView` unchanged. Header: back button, record title, subtitle `user.preview`, and the two tabs.
- **Status hint** (`headerPartial: hint`, server-rendered with the `.summer-callout` kit exactly as sm-user-plugin's `_hint.htm`): one callout or nothing, by precedence:
1. deleted → `summer-callout--danger`, `user.trashed_hint_title` / `user.trashed_hint_desc`;
2. deactivated → `summer-callout--warning`, `user.deactivated_hint_title` / `user.deactivated_hint_desc`;
3. the record is the signed-in administrator's own → `summer-callout--warning`, `user.self_hint_title` / `user.self_hint_desc`;
4. otherwise no callout.
The callout has `role="status"`; the hint is refetched after every record action (existing `hintKey`).
- Fields shown: Account tab `login`, `email`, `first_name`, `last_name`, `role` (relation text box), `last_login` (read-only datepicker); Permissions tab `is_superuser` (Yes/No pill, superusers only) and `permissions` (read-only editor, no lock icons on a read-only screen, existing rule). Password fields never show (existing preview rule).
- **Footer actions, left to right inside the right-aligned group**: the owner's record actions in declared order (`activate`, `deactivate`, `restore`, each only while it applies), then contributed actions (D-14) in boot merge order, then the primary **Edit administrator** button. All record actions are outline buttons (existing `RecordActions`). Applies rules:
- `activate`: not deleted and not activated;
- `deactivate`: not deleted, activated, and **not the editor's own record** (the action is not offered to yourself; the server still refuses it with `user.self_deactivate`);
- `restore`: deleted.
So a record shows at most one of Activate / Deactivate / Restore. A record action that targets the last active superuser is still offered and refused with `user.last_superuser` (the check needs the lock and is not a pure read).
- Every action asks for confirmation (existing). Success: toast with the action's success phrase, record and hint reload. 403: danger toast with the server message (C8). 409: existing stale toast and reload.
- A contributed action with `Danger: true` uses the danger confirm button; its footer button stays outline (existing rule).
### C5. Roles list and form (D-16, D-17)
List (`config_list.yaml`): `title: user.role.list_title`, `recordUrl` → the record's update form (Roles has **no preview block**), toolbar `[create, delete]`, `showCheckboxes: true`, no bulk actions, `noRecordsMessage: user.role.list_empty`, messages `create: user.role.new`, `deleteConfirm: user.role.delete_selected_confirm`, `deleted: messages.list.deleted`.
| Column | Type | Visible | Searchable / sortable |
|--------|------|---------|----------------------|
| `name` | text | yes | searchable, sortable |
| `code` | text | yes | searchable, sortable |
| `description` | text | yes | searchable |
| `is_system` | switch | yes | sortable |
| `created_at` | datetime | invisible | sortable |
| `updated_at` | datetime | invisible | sortable |
No users-count column in this phase (cabana columns have no count/`valueFrom`; D-17's refusal names the count instead).
Form (`config_form.yaml`): `name: user.role.name` (en "Role", pl "Rola"), messages `create: user.role.new`, `update: user.role.edit`, `saved: messages.form.saved`, `deleteConfirm: user.role.delete_confirm`, `deleted: messages.form.deleted`. Create redirects to the new role's update form; Save and close returns to the list.
| Field | Type | Span | Tab | Notes |
|-------|------|------|-----|-------|
| `name` | text | left | default (`form.tab_default`) | Required. Comment `user.role.name_comment` |
| `code` | text, `preset: { type: slug, field: name }` | right | default | Required. Comment `user.role.code_comment`. Follows the name on create until edited (existing preset rule); never follows on update. A change on a system role is refused with `user.role.code_locked` on the `code` field |
| `description` | textarea, `size: tiny` | full | default | |
| `permissions` | permissioneditor, `mode: checkbox` | full | `user.permissions` | Label `user.role.permissions_field`, comment `user.role.permissions_comment`. Code-declared grants render record-locked (C7, note `permissioneditor.locked_by_code`); permissions the editor lacks render request-locked (existing, note `permissioneditor.locked`) |
### C6. Field `trigger` (D-21, used by D-06)
**YAML and schema.** `trigger` becomes a valid key in `formFieldKeys`:
```yaml
trigger:
action: disable # disable | hide
field: is_superuser # another field of the same form (same fields.yaml)
condition: checked # checked | unchecked
```
- Boot errors (fail fast, like every unknown key): a missing sub-key; an action other than `disable`/`hide`; a condition other than `checked`/`unchecked` (Winter's `value[...]` is not supported in this phase); a `field` that names no field of the same form, names itself, or names a field whose type is not `switch` or `checkbox`; `trigger` on a field nested in a repeater form.
- The form schema payload carries it as `trigger: { action, field, condition }` on `cabana.FormField` (swag annotation → `admin/openapi/admin.json` → `schema.d.ts`, `scripts/check-admin-openapi.sh`).
**Evaluation (SPA).** One pure helper (suggested `admin/src/components/form/trigger.ts`) takes the field, the **whole form's** values (FormView `values`, not the active tab's scope, because the controlling field may sit on another tab) and the set of field names present in the filtered schema, and returns `'active' | 'inactive'`:
- `checked`: active when the controlling value is on by `toggleOn` (`true`, `1`, `"1"`, `"true"`);
- `unchecked`: active when it is off;
- a controlling field absent from the schema for this request (for example `is_superuser` removed for a non-superuser, D-03) or excluded by `context` counts as **off**.
It re-evaluates on every value change, immediately (no animation, no debounce).
**`disable` while active (FormView):**
- The field renders through its own control **as read-only** (the control receives the field with `readOnly: true`, exactly as `PreviewField` does), keeping its current values visible. For `permissioneditor` this disables every segment / checkbox and, by the existing rule, hides the per-row lock icons and lock notes.
- A note line renders in `FormField` **between the label and the control** (so it is visible above a long permission editor): `<p id="{controlId}-trigger" data-trigger-note class="flex items-center gap-2 text-[13px] text-muted">` with `Info` 14px (`shrink-0`, `aria-hidden`) and `form.trigger_disabled_on` (condition `checked`) or `form.trigger_disabled_off` (condition `unchecked`), `:field` = the controlling field's label. Its id joins the control's `aria-describedby`.
- **Payload:** the field is left out of the save body (`editablePayload` skips it). The server then keeps the stored value: an absent `permissioneditor` key is not lifted (`liftPermissionValues` skips absent keys) and an absent scalar is not filled. Values typed before the field was disabled stay in the form and return when it is enabled again.
- **Announcement:** when a user edit flips a trigger, the form's existing polite live region (`announce`) says "<field label>: <note text>" on disable, and nothing on enable.
**`hide` while active (FormView):**
- The field is not rendered, is left out of the save body, and is left out of the tab computation: a tab whose every field is hidden disappears, and if it was the active tab the first remaining tab becomes active.
- Its name is also left out of `FormErrorBanner`'s `fieldNames`, so a server message for a hidden field is shown in the banner as an orphan line rather than lost.
**PreviewView:** `hide` is evaluated the same way against the record's values; `disable` has no effect (every preview field is already read-only) and no note is shown.
**Dirty state:** because the payload changes when a trigger flips, toggling the controlling field marks the form dirty exactly once per real change (existing snapshot comparison); no extra rule.
**D-06 outcome on the Administrators form:** a superuser opens another superuser's record → Permissions tab shows the Superuser card on, then "Permission overrides" with the note "Not editable while “Superuser” is on." and a read-only editor. Turning the switch off enables the editor at once. A non-superuser never sees the switch and always gets an editable editor (subject to D-07 locks).
### C7. Per-record locked permission values (D-21, used by D-16 and D-08)
**Data.** The record response carries, per `permissioneditor` field, the codes the server fixes for **this record**, with the value to show and the note to show. Recommended shape (planner may rename; the rendering rules below are fixed): `meta.permissionLocks: { [fieldName]: Array<{ code: string, value: number, note: string }> }`, where `note` is already translated in the request locale. It is absent when no record lock applies, and it is never part of the form schema (the schema is fetched without a record id). `FormView` (and `PreviewView`) pass it to the field through the same path as `meta.labels` (prop through `FormGrid`/`FormField`/`FieldRenderer`, or a provided context; planner's call).
**Server use in this phase:**
- Roles (D-16): every code-declared grant of the role (`pact.Permission.Roles` for its code), `value: 1`, note `permissioneditor.locked_by_code`.
- Administrators (D-08): on the editor's own record, every offered code, `value` = the stored override (1, -1, or 0 for inherit), note `permissioneditor.locked_self`.
**Rendering in `PermissionEditorField`** (editable screens):
- A record-locked row shows `value` (checkbox checked for 1; the radio segment for 1 / 0 / -1), its control is disabled with `aria-disabled="true"`, the existing `Lock` 14px icon follows the label, and the note line (`text-[13px] text-muted`, existing lock-note markup and id, linked by `aria-describedby`) shows the record lock's `note` instead of `permissioneditor.locked` or the option comment. The row gets `data-locked` and `data-locked-by="record"`.
- When a row is both request-locked (the editor lacks the code) and record-locked, the record lock's value and note win.
- On a read-only screen (preview, or a trigger-disabled editor) record locks render like every lock there: value shown, no icon, no note (existing `showsLock` rule).
**Payload.** Record-locked codes are **never sent**, exactly like non-offered codes (`permissionValues` skips them). The server keeps whatever it stores for them and skips them in the locked-value comparison, so a role whose code grant is not in its stored JSON saves without a 403. The displayed value comes from the lock, not from the stored JSON.
### C8. Where each refusal surfaces
The SPA shows a 403 save refusal as the `FormErrorBanner` forbidden banner (`error.message`, else `form.forbidden`) plus field lines for `error.details`; a 422 as field lines plus the error-count banner; and a delete or action refusal as a danger toast with `error.message`. A `ValidationError` has no message of its own (the envelope says "Validation failed"), so **every refusal that can come back from a delete or an action must be a `ForbiddenError` with `Message` set**, or its text is lost in the toast.
| Rule | Path | Status / error | Renders as | Copy |
|------|------|----------------|-----------|------|
| D-03 hidden superuser opened by id | GET record | 404 | Existing `form.load_failed` alert on the form/preview | existing |
| D-05 `is_superuser` sent by a non-superuser | create / update | 403, `Message` + `Details.is_superuser` | Forbidden banner | `user.superuser_forbidden` |
| D-07 locked permission changed | create / update | 403 (existing `applyPermissionValues`) | Forbidden banner + line under the editor | `permissioneditor.locked` |
| D-07 locked role assigned | create / update | 403 (existing `checkRelationLocks`) | Forbidden banner + line under Role | `user.role_ceiling` (`RelationLock.Message`) |
| D-08 own role / superuser / permissions changed | update | 403, `Message` + `Details` on the changed field | Forbidden banner + field line | `user.self_privileged` |
| D-08 delete yourself | form delete, list bulk delete | 403, `Message` | Danger toast | `user.self_delete` |
| D-08 deactivate yourself | bulk action (record action is not offered) | 403, `Message` | Danger toast | `user.self_deactivate` |
| D-09 last active superuser | update (demote) | 403, `Message` + `Details.is_superuser` | Forbidden banner + line under the switch | `user.last_superuser` |
| D-09 last active superuser | delete, bulk delete, deactivate (record / bulk) | 403, `Message` | Danger toast | `user.last_superuser` |
| D-19 target outranks the editor | update | 403, `Message` | Forbidden banner | `user.outranked` |
| D-19 target outranks the editor | delete, bulk delete, record / bulk actions | 403, `Message` | Danger toast | `user.outranked` |
| D-20 own password on this screen | update | 403, `Message` + `Details.password` | Forbidden banner + line under Password | `user.own_password` |
| Login / email taken (active or deleted) | create / update | 422, `Details.login` / `Details.email` | Field line + count banner | `user.login_taken[_deleted]`, `user.email_taken[_deleted]` |
| Password rules (required on create, min 4, max 72 bytes, confirmed) | create / update | 422, `Details.password` | Field line | existing validation messages |
| D-16 system role code changed | update | 422, `Details.code` | Field line | `user.role.code_locked` |
| Role code taken | create / update | 422, `Details.code` | Field line | `user.role.code_taken` |
| D-17 role still has admins | form delete, bulk delete | 403, `Message` (see note) | Danger toast | `user.role.delete_has_users` with `:name`, `:count` |
| D-17 system role | form delete, bulk delete | 403, `Message` | Danger toast | `user.role.delete_system` |
Note on D-17: CONTEXT says "validation error". The visible requirement is that the toast names the count. With the current SPA that needs `ForbiddenError{Message}`; a `ValidationError` would show "Validation failed". The planner either uses the 403 shape or changes the SPA delete toast to read `error.details`. The 403 shape is recommended because it needs no SPA change. One refused row in a bulk delete rolls back the whole selection (existing), and the toast names the first refused role.
---
## UI Considerations
Applicable state considerations resolved: 9 elements (probe-checked 2026-10-21; 7 gap rows added after the probe) (Administrators list, Administrators form, Administrators preview with record actions, Roles list, Roles form, settings tiles, trigger mechanism, record permission locks, refusal surfaces). Copy for empty and error rows lives in the Copywriting Contract.
| Category | Element(s) | Status | Resolution / Reason |
|----------|------------|--------|---------------------|
| empty | Administrators list | ✅ covered | With no rows (search or filters) the list shows `user.list_empty`; an empty search shows the existing `messages.list.empty_search` + hint. Without search or filters the list always holds at least the viewer's own account, so the empty copy only shows for filtered views |
| empty | Roles list | ✅ covered | `user.role.list_empty`; the seeded system roles mean an unfiltered list is never empty |
| loading | Both lists, both forms, preview | ✅ covered | Existing skeletons and disabled footers in `ListView`/`FormView`/`PreviewView`; no new loading UI |
| error | Both lists, both forms, preview | ✅ covered | Existing `list.load_failed` / `form.load_failed` alerts; a hidden superuser opened by id (D-03) lands in the same alert |
| populated | Administrators list | ✅ covered | Columns login, first name, last name, email, role, last sign-in, superuser (superusers only); deleted and deactivated rows carry text badges |
| partial | Administrators list (mixed states) | ✅ covered | Active, deactivated, deleted and deleted+deactivated rows coexist; each state is a text badge in fixed order (WCAG 1.4.1) |
| zero-one-many | Bulk actions | ✅ covered | Bulk menu is enabled only with a selection (existing); one refused row rolls back the whole selection and the toast shows the refusal |
| overflow | Both lists | ✅ covered | Existing pagination (20 per page) and horizontal table scroll; long emails wrap per existing `CellValue` rules |
| long-text | Role name / record title in confirms and toasts | ✅ covered | `ConfirmDialog` description wraps with `[overflow-wrap:anywhere]` (added in 16.3); the record title truncates in the header h1 (existing `truncate`) |
| populated | Administrators form, Account tab | ✅ covered | Login/email, first/last name, password/confirmation pairs, full-width role picker with locked roles shown locked |
| partial | Administrators form, superuser target | ✅ covered | Permissions tab shows the Superuser card on and the overrides editor read-only with the trigger note (C6) |
| partial | Administrators form, own record | ✅ covered | Every override row is record-locked with `permissioneditor.locked_self` (C7); a change of role or the Superuser switch is refused with `user.self_privileged` in the forbidden banner; a password change is refused with `user.own_password` under Password |
| partial | Administrators form, non-superuser editor | ✅ covered | No Superuser switch, column or filter; permissions the editor lacks are locked rows; roles above the editor's ceiling are locked options |
| empty | Permission editor with no catalog | ✅ covered | Existing `permissioneditor.empty` box; cannot happen in practice once the two `backend.*` codes are registered |
| overflow | Permission editor (many plugin permissions) | ✅ covered | Sections per catalog tab, page scroll; the trigger note sits above the editor so it stays visible without scrolling to the comment |
| error | Save refused (403) | ✅ covered | Forbidden banner with the server message, field lines for named fields, values and dirty state kept (existing); mapping in C8 |
| error | Save invalid (422) | ✅ covered | Field lines, count banner, first invalid field's tab activated and focused (existing) |
| populated | Administrators preview | ✅ covered | Status callout by precedence deleted > deactivated > own account; footer with at most one of Activate / Deactivate / Restore, then contributed actions, then Edit |
| error | Record action refused (403) / stale (409) / gone (404) | ✅ covered | Danger toast with server message; 409 existing stale toast and reload; 404 existing load-failed state |
| zero-one-many | Contributed record actions | ✅ covered | Zero: only owner actions; one or many: outline buttons after the owner's, in boot merge order, wrapping with the footer's `flex-wrap` |
| long-text | Contributed action labels | 🧪 backstop | An SPA test renders the preview footer with three owner-style actions plus two contributed actions with 40-character labels at 1024px and asserts the footer wraps without overlapping the Edit button |
| populated | Roles form, system role | ✅ covered | Code-declared grants checked and locked with `permissioneditor.locked_by_code`; extra grants editable within the D-07 ceiling; a code change refused under Code |
| populated | Settings tiles | ✅ covered | Two tiles under "System", each shown only to holders of its permission (or superusers) |
| populated | Trigger `disable` | 🧪 backstop | An SPA unit test: a radio permission editor with `trigger: {action: disable, field: is_superuser, condition: checked}` is read-only with the note when the switch is on, editable when off, and its key is absent from `editablePayload` while disabled |
| partial | Trigger, controlling field absent | 🧪 backstop | An SPA unit test: with `is_superuser` missing from the schema the editor is editable and sent |
| partial | Trigger `hide` on a whole tab | 🧪 backstop | An SPA unit test: hiding every field of the active tab removes the tab, activates the first tab, and a server error on the hidden field appears in the banner |
| populated | Record locks | 🧪 backstop | An SPA unit test: a checkbox editor with `permissionLocks` shows the locked code checked and disabled with the record note, and `permissionValues` omits it from the payload |
| empty | Administrators create form | ✅ covered | Blank form; password and confirmation are required on create (D-11); missing or mismatched fields come back as 422 field lines with the count banner (existing) |
| long-text | Administrators and Roles form inputs | ✅ covered | Existing full-width text inputs; long login, email, name or description values scroll inside the input; the header h1 record title truncates (existing `truncate`) |
| empty | Roles create form | ✅ covered | No record locks on a new role; the checkbox editor starts all unchecked and editable within the D-07 ceiling (codes the editor lacks are locked rows) |
| error | Roles form refusals | ✅ covered | A system role's code change is refused under Code (D-16); deleting a role that still has admins returns 403 with `user.role.delete_has_users` in a danger toast (D-17, C8) |
| zero-one-many | Roles bulk delete | ✅ covered | One selected role that still has admins (or is a system role) refuses the whole selection; the transaction rolls back and the danger toast names that role |
| error | Invalid `trigger` YAML | ✅ covered | Unknown `action`, `condition` or a `field` not in the form is a boot error naming controller, field and key; it never reaches the SPA |
| zero-one-many | Record permission locks | ✅ covered | No locks: plain editor. Some locks: those rows are locked with the record's note, others editable. All locked (editing your own record, D-08): the whole editor is read-only |
| loading / empty | Trigger note, lock note | ➖ dismissed | Static text derived synchronously from schema and record; no async state |
---
## Registry Safety
| Registry | Blocks Used | Safety Gate |
|----------|-------------|-------------|
| shadcn official | none (shadcn not used) | not applicable |
| third-party registries | none | not applicable |
| npm dependencies | none added (Reka UI, `@lucide/vue`, Tailwind already installed at the versions above) | not applicable |
---
## Checker Sign-Off
- [ ] Dimension 1 Copywriting: PASS
- [ ] Dimension 2 Visuals: PASS
- [ ] Dimension 3 Color: PASS
- [ ] Dimension 4 Typography: PASS
- [ ] Dimension 5 Spacing: PASS
- [ ] Dimension 6 Registry Safety: PASS
- [ ] Dimension 7 Inventory Provenance: PASS
**Approval:** pending

View File

@@ -0,0 +1,95 @@
---
phase: "19.6.1"
slug: "backend-administrators-screen"
# status lifecycle: draft (seeded by plan-phase) → validated (set by validate-phase §6)
# audit-milestone §5.5 distinguishes NOT-VALIDATED (draft) from PARTIAL (validated + nyquist_compliant: false) (#2117)
status: draft
nyquist_compliant: false
wave_0_complete: false
created: "2026-10-21"
---
# Phase 19.6.1 — Validation Strategy
> Per-phase validation contract for feedback sampling during execution.
---
## Test Infrastructure
| Property | Value |
|----------|-------|
| **Framework** | Go `testing`; testcontainers-go Postgres via `adminGorm(t)` (skips under `-short`) |
| **Config file** | none — package helpers `adminGorm`, `adminHandler`, `insertAdmin` in `modules/cabana/auth_test.go` |
| **Quick run command** | `go vet ./modules/cabana ./modules/pact && go test -short ./modules/cabana ./modules/pact ./modules/surf` |
| **Full suite command** | `go vet ./... && go test ./... && (cd examples/hello && go test ./...) && go test ./cmd/summer -run TestDocsTree && go run ./cmd/summer docs:build --check` |
| **Estimated runtime** | ~180 seconds (Docker-backed) |
---
## Sampling Rate
- **After every task commit:** Run the quick run command
- **After every plan wave:** `go test ./modules/cabana ./modules/surf` (Docker) + `cd examples/hello && go test ./...`
- **Before `/msd-verify-work`:** Full suite must be green
- **Max feedback latency:** 180 seconds
---
## Per-Task Verification Map
Filled by the planner per task; the decision → test map lives in `19.6.1-RESEARCH.md` § Validation Architecture.
| Task ID | Plan | Wave | Requirement | Threat Ref | Secure Behavior | Test Type | Automated Command | File Exists | Status |
|---------|------|------|-------------|------------|-----------------|-----------|-------------------|-------------|--------|
| 19.6.1-01-01 | 01 | 1 | D-01, D-02, D-04, D-14, D-15 | T-19.6.1-09, -10, -14 | seam merged before permission validation; contributed actions behind target + own permissions; no default role grant | smoke (-short + Postgres) | `go test -short -run '^TestBackendScreensSmoke$' ./modules/cabana`; `go test -run '^TestBackendScreensPostgresSmoke$' ./modules/cabana`; `go -C examples/hello test ./...` | ❌ W0 | ⬜ pending |
| 19.6.1-01-02 | 01 | 1 | D-03, D-05..D-08, D-11, D-12, D-19, D-20 | T-19.6.1-01..-05, -07, -08, -11, -13, -17 | save-path rules in the save tx; is_superuser projected; hidden for non-superusers | smoke (Postgres) | `go test -run '^TestAdministratorsFormSmoke$' ./modules/cabana` | ❌ W0 | ⬜ pending |
| 19.6.1-01-03 | 01 | 1 | D-08, D-09, D-10, D-12, D-19 | T-19.6.1-05, -06, -11, -12 | last-superuser guard under advisory lock; soft delete only; revocation | smoke (Postgres) | `go test -run '^TestAdministratorsLifecycleSmoke$' ./modules/cabana` | ❌ W0 | ⬜ pending |
| 19.6.1-01-04 | 01 | 1 | D-02, D-07, D-16, D-17 | T-19.6.1-04, -15 | roles ceiling; system role code locked; in-use delete refused | smoke (Postgres) | `go test -run '^TestRolesSmoke$' ./modules/cabana`; `scripts/check-admin-openapi.sh --check` | ❌ W0 | ⬜ pending |
| 19.6.1-02-01 | 02 | 2 | D-21 (D-16, D-08) | T-19.6.1-19, -20, -22, -23 | record-locked codes kept server-side; changed value 403 | smoke (-short) + vitest | `go test -short -run '^TestPermissionRecordLocksSmoke$' ./modules/cabana`; `scripts/check-admin-openapi.sh --check`; `npm --prefix admin run test` | ❌ W0 | ⬜ pending |
| 19.6.1-02-02 | 02 | 2 | D-21 (D-06) | T-19.6.1-18, -21, -22, -23 | trigger boot errors; disabled field not sent; dist matches build | smoke (-short) + vitest + dist | `go test -short -run '^TestFieldTriggerSmoke$' ./modules/cabana`; `npm --prefix admin run test`; `scripts/check-admin-dist.sh` | ❌ W0 | ⬜ pending |
| 19.6.1-03-01 | 03 | 3 | D-05, D-18 | T-19.6.1-24, -26 | harness + first rule over HTTP | Postgres | `go test -run '^TestAdministratorsSuperuserFlag$' ./modules/cabana` | ❌ W0 | ⬜ pending |
| 19.6.1-03-02 | 03 | 3 | D-03, D-06, D-07, D-08, D-11, D-19, D-20 | T-19.6.1-24 | each save-path rule has a failing-without-rule test | unit (-short) + Postgres | `go test -short -run '^TestRoleCeiling$' ./modules/cabana`; `go test -run '^TestAdministrators(SuperuserHidden\|SuperuserProjected\|PermissionCeiling\|RoleCeiling\|SelfEdit\|PasswordAndIdentity\|Outranked\|OwnPassword)$' ./modules/cabana` | ❌ W0 | ⬜ pending |
| 19.6.1-03-03 | 03 | 3 | D-04, D-09, D-10, D-12, D-14, D-16, D-17 | T-19.6.1-24, -25, -26 | held-lock race on 5 paths; revocation; seam boot and run; roles | unit (-short) + Postgres | `go test -short -run '^(TestActionExtensionBoot\|TestBackendScreensActivation)$' ./modules/cabana`; `go test -run '^(TestAdministratorsLastSuperuser\|TestAdministratorsLastSuperuserConcurrent\|TestAdministratorsLifecycle\|TestAdministratorsRevocation\|TestActionExtensionRun\|TestRolesLifecycle\|TestRolesPermissionCeiling)$' ./modules/cabana` | ❌ W0 | ⬜ pending |
| 19.6.1-03-04 | 03 | 3 | D-21, D-18 | T-19.6.1-24, -27 | D-21 Go + SPA tests incl. backstops; fail-closed gate | unit + vitest + browser + gate | `go test -short -run '^(TestFieldTriggerBoot\|TestPermissionRecordLocks)$' ./modules/cabana`; `npm --prefix admin run test`; `npm --prefix admin run test:browser`; `bash scripts/check-phase19.6.1.sh --all` | ❌ W0 | ⬜ pending |
*Status: ⬜ pending · ✅ green · ❌ red · ⚠️ flaky*
---
## Wave 0 Requirements
- [ ] `modules/cabana/backend_screens_test.go` — activation and schema (`-short`; smoke in plan 01, `TestBackendScreensActivation` in plan 03)
- [ ] `modules/cabana/backend_screens_smoke_test.go` — plan 01 Postgres smokes
- [ ] `modules/cabana/record_locks_smoke_test.go`, `modules/cabana/field_trigger_smoke_test.go` — plan 02 smokes (`-short`)
- [ ] `modules/cabana/action_extensions_test.go` (boot, `-short`) and `modules/cabana/action_extensions_postgres_test.go` (run) — D-14
- [ ] `modules/cabana/backend_privileges_internal_test.go` — subset/ceiling helpers (`package cabana`)
- [ ] `modules/cabana/administrators_harness_test.go` — superuser-capable seeding, roles, login, `isolateSuperusers`
- [ ] `modules/cabana/administrators_postgres_test.go` — D-03, D-05..D-08, D-11, D-19, D-20 at HTTP level
- [ ] `modules/cabana/administrators_lifecycle_postgres_test.go` — D-09 held-lock race, D-10, D-12
- [ ] `modules/cabana/roles_postgres_test.go` — D-07 (roles), D-16, D-17, D-21 code-declared locks
- [ ] `modules/cabana/field_trigger_test.go`, `modules/cabana/record_locks_test.go` — D-21 (`-short`)
- [ ] `admin/tests/form/trigger.test.ts` + additions to PermissionEditorField, formState, FormView, PreviewView tests; `admin/tests/browser/record-actions-wrap.test.ts`
- [ ] `scripts/check-phase19.6.1.sh` — phase gate
- [ ] `modules/cabana/export_test.go` — `SuperuserLockKey`
---
## Manual-Only Verifications
| Behavior | Requirement | Why Manual | Test Instructions |
|----------|-------------|------------|-------------------|
| Administrators/Roles screens render and save in the admin SPA | SC-1 | visual SPA check | log in as superuser and as a limited admin, open Settings → Administrators / Roles, create, edit, deactivate |
---
## Validation Sign-Off
- [ ] All tasks have `<automated>` verify or Wave 0 dependencies
- [ ] Sampling continuity: no 3 consecutive tasks without automated verify
- [ ] Wave 0 covers all MISSING references
- [ ] No watch-mode flags
- [ ] Feedback latency < 180s
- [ ] `nyquist_compliant: true` set in frontmatter
**Approval:** pending

View File

@@ -0,0 +1 @@
No external API integration: the phase adds cabana's own Administrators and Roles admin screens, a framework-internal pact capability and SPA support for its own admin JSON API; no third-party API, SDK or service is integrated.

View File

@@ -108,6 +108,23 @@ Out of scope: MCP resources (the capability is designed for them, but they ship
fonoteka.go and sm-bm-app must still build and pass at the bumped pins. Push, tag and repo work need the user's go-ahead.
### Decisions after research (2026-10-21, plan-phase)
- **D-32:** **Hybrid protocol.** pier also answers MCP 2026-07-28 clients, which have no sessions, through a stateless path next to D-25's stateful one (go-sdk v1.8.0 serves 2026-07-28 only statelessly). Both paths share identity, dispatch and per-principal filtering. Push for stateless clients follows the 2026-07-28 mechanism (`subscriptions/listen`). Amends D-25.
- **D-33:** **pier is compiled in only with a build tag** (for example `summercms_mcp` in `summer.yaml` `build.tags`, the `summercms_s3` pattern). Without the tag the binary carries no go-sdk. With the tag, the config flag still keeps pier off by default (D-30). Plugin-facing `pact` types stay SDK-free, so plugins compile either way. Amends D-30 and SC-1.
- **D-34:** **Admins set and remove links on the Administrators screen from Phase 19.6.1**, through its record-action seam. It is no longer a cabana "backend Users" screen 19.7 has to invent. Amends D-04 part 2 and D-07.
- **D-35:** Defaults taken from research:
- frontend tool filtering uses group grants, matching what `user.permission` enforces (not `MergedPermissions`);
- sessions bind to `(client_id, user_id)`, so they survive token refresh;
- the `ai` scope plays no part in the read/write ceiling;
- consent is served as JSON endpoints in sm-oauthserver-plugin, with a `/connect` page in vue-demo-app;
- the permission code is `golem15.users.manage_backend_links`.
- **D-36:** **Plan split, 5 plans** (user-confirmed):
1. framework seams;
2. the pier module and framework release;
3. sm-user-plugin;
4. oauthserver, Journal and demo;
5. unit tests, `check-phase19.7.sh` and the security review.
### Claude's Discretion
- Exact Go identifiers: the pact capability and tool/prompt struct names, the link service, the context marker, the table name and the permission code.
- Config key spellings (enable flag, endpoint path with default `/mcp`, the personal-token flag, the session TTL).

View File

@@ -0,0 +1,688 @@
# Phase 19.7: Generic MCP module - Research
**Researched:** 2026-10-21
**Domain:** MCP streamable HTTP server inside a Go binary (official go-sdk), in-process route dispatch through surf, OAuth resource-server verification, frontend/backend identity link
**Confidence:** HIGH for in-repo seams and SDK behaviour (source read and probed this session); MEDIUM for MCP-ecosystem direction (spec 2026-07-28); LOW for client-compatibility claims (marked [ASSUMED])
<user_constraints>
## User Constraints (from CONTEXT.md)
### Locked Decisions
#### Prerequisites and sequencing
- **D-01:** **Both prerequisite todos are folded into 19.7**, as early plans before the MCP module itself: `2026-10-17-plugin-registered-frontend-permissions` and `2026-10-17-frontend-backend-account-link`. One sm-user-plugin release, one pin bump per app, and one security review cover the whole identity chain.
- **D-02:** **19.7 runs after 19.4 is complete.** OAuth for MCP comes from `sm-oauthserver-plugin`'s MCP `wristband.Server` (`/oauth/mcp/*`, DCR, 19.4 D-01/D-02/D-06). 19.7 adds no second OAuth adapter. It adds the `backend` scope (D-21) and resolves the backend principal next to the frontend one.
- **D-03:** **The account link lives in sm-user-plugin.** That covers:
- the table, for example `golem15_user_backend_links` (name is the planner's call);
- the proof round-trip;
- a new backend permission (for example `golem15.user.manage_backend_links`);
- the admin set/remove action;
- an `app.Lookup` service the MCP module calls, for example `LinkedBackendUser(ctx, frontendUserID)` (name is the planner's call).
Only sm-user-plugin can verify a frontend sign-in (password, sociallogin, the mfa gate), and sm-oauthserver-plugin already `Requires()` it. The framework never imports the user plugin. — **Reversibility:** costly — the table and service become part of a core plugin's contract consumed by apps and pier.
- **D-04:** **cabana gains an additive, generic seam** so that a plugin can contribute:
- a section or action to **My Profile** (`cabana/account.go` and the admin SPA account page), which sm-user-plugin uses for "Link frontend account";
- a record action on the **backend Users** screen (set/remove the link).
The seam is generic and names neither the user plugin nor MCP. Existing cabana payloads are unchanged. The researcher checks whether the 19.6 D-15 "Reset 2FA" action needs the same seam, and reuses it if so.
- **D-05:** **The link is proven by a site sign-in round-trip.** "Link frontend account" creates a one-shot, signed, short-lived link request bound to the admin. The admin is sent to the site login with it. The frontend user signs in by any method the site supports (password, social login, 2FA), and the request is confirmed. Only then is the link saved. Linking by matching email, or by picking a user from a list without proof, is never allowed. — **Reversibility:** one-way — a link created without proof lets an admin act as that frontend user through MCP; unproven links cannot be told apart afterwards.
- **D-06:** **Cardinality 1:1**, unique on both sides: a frontend user has at most one backend link, and a backend user has at most one frontend link.
- **D-07:** **Admin management behind the new backend permission.** A holder can remove any link and can set one for editor onboarding. A link set by an admin records `linked_by` and fires an event. The self-service My Profile path always needs the D-05 proof.
- **D-08:** **The link is resolved on every request, not stored in the OAuth grant.** Unlinking, deleting either user, or revoking the grant takes effect on the next MCP call with no token surgery. One indexed query per request, which may be cached briefly; the planner decides and states the staleness bound.
#### Frontend permission codes (todo)
- **D-09:** A new optional `pact` capability is the frontend counterpart of `pact.HasPermissions`. A plugin returns codes with label, tab and comment. sm-user-plugin offers them in the user and group permission editors next to the rows seeded in `golem15_user_frontend_permissions`. Additive only: existing seeded rows, the table, the codes and the payloads stay as they are (core-plugin rule).
#### Tool declaration and dispatch
- **D-10:** **A tool binds to a route by method + path** (for example `POST /_journal/api/v1/posts`), resolved at boot in surf's route table. **Boot fails** if the route does not exist. The call goes **in-process through that route's full middleware chain** (throttles, scope, permission), so it runs the same code path as HTTP. A tool carries no handler func.
- **D-11:** **Arguments map through one input struct with tags.** `path:"id"` fills `{id}`, `query:"q"` goes to the query string, and the remaining `json` fields form the body. The input JSON schema is reflected from the same struct, with `jsonschema` tags supplying descriptions.
- **D-12:** **Names are auto-prefixed with the plugin short code.** A plugin declares `create_post`, and pier exposes `journal_create_post`. A duplicate within one plugin is a boot error. Prompts follow the same rule. — **Reversibility:** costly — clients and saved agent configs reference tool names once shipped.
- **D-13:** **Results pass the route's JSON through.**
- A 2xx body is returned as text content plus `structuredContent`.
- An optional Output struct produces an `outputSchema`.
- A 4xx/5xx becomes `isError: true` carrying the route's own error body, so the agent sees the real validation errors.
- **D-14:** **Annotations are derived from the HTTP method and can be overridden.** GET gives `readOnlyHint`, DELETE gives `destructiveHint`, and PUT gives `idempotentHint`. A tool may override any hint.
- **D-15:** **Only token-guarded routes can be targets.** At boot the route must either carry the guard's token-accepting middleware (`user.api_token` for `user`, the cabana backend token guard for `backend`), or its tool must explicitly declare a guard. The second case covers routes that authenticate inside the handler. A cookie/CSRF-only or public route can't become a tool by accident.
- **D-16:** **Permissions = route middleware ∪ tool declaration.** pier derives the requirements from route middleware where present (`user.scope:*`, `user.permission:<code>`, cabana scope/permission). A tool may also declare `Permissions`, which `tools/list` checks too. Journal is the case that needs this: `POST /_journal/api/v1/posts` has only `throttle:journal-api` and checks `cabana.Allows` inside `requireBackendPrincipal`. The declaration only filters the list, and the route still enforces on every call.
- **D-17:** **Identity reaches the handler as a pre-resolved principal on the context.** pier sets the resolved principal (`bouncer.WithUser` / the user plugin's equivalent) behind a framework-owned marker that only an in-process pier dispatch can set; it must not be settable from an HTTP request. The following honour that marker before parsing the Authorization header, additively:
- bouncer;
- sm-user-plugin's `user.api_token` middleware;
- Journal's `requireBackendPrincipal`.
No credential is minted per call. Security review covers the marker. — **Reversibility:** costly — three auth paths in two core plugins learn the marker.
- **D-18:** **The first tools ship in this phase.** Journal declares backend post tools (list/show/create/update, plus whatever research finds useful), and sm-user-plugin declares a few frontend tools (for example own profile). sm-demo-app enables pier and mounts both, which proves both guards end to end.
#### Session credentials and filtering
- **D-19:** **Personal tokens can open an MCP session behind a config flag**, off by default (spelling is the planner's call, for example `pier.allow_personal_tokens`).
- A `user_api_tokens` bearer yields frontend tools only.
- A cabana `g15_…` token yields backend tools only.
- Each is capped by that token's own scope, exactly as over HTTP.
OAuth is always available.
- **D-20:** **OAuth `read`/`write` are a ceiling.** A grant with only `read` offers only tools with `readOnlyHint`, and `write` unlocks the rest. Permissions always filter on top: a scope never grants anything.
- **D-21:** **Backend tools need a separate `backend` OAuth scope.** A client must request `backend`. The consent screen shows a line such as "also act as admin <name>" only when the user has a link. A user can therefore connect a client to frontend tools only, and the scope is a visible, revocable ceiling. This adds the scope to sm-oauthserver-plugin's MCP Server (additive to 19.4's `read`/`write`/`ai`/`offline_access`). — **Reversibility:** costly — published scope name in the MCP metadata and in existing grants.
- **D-22:** **The backend principal keeps the linked admin's real cabana permissions**, superuser included, capped by the grant's read/write scope (D-20). The security review covers superuser reach through MCP.
- **D-23:** **A call runs as the tool's declared guard.** Each tool has exactly one guard: a `backend` tool runs as the linked admin, and a `user` tool runs as the frontend user. `tools/list` is the union of what each identity may call. Audit and actor attribution name the real actor.
#### Transport, SDK and features
- **D-24:** **The SDK is the official `github.com/modelcontextprotocol/go-sdk`.** A decision note under `.planning/notes/` names it and its pinned version before code lands, and `.planning/research/STACK.md` is updated (CLAUDE.md dependency rule). Research also:
- checks the transitive dependencies, including `google/jsonschema-go` for schema reflection;
- confirms that the streamable HTTP handler supports the session-store and auth hooks needed for D-25.
- **D-25:** **Stateful streamable HTTP with Redis-backed sessions** (`Mcp-Session-Id`, SSE streams), shared across instances on 19.3's Redis.
- A session never replaces the bearer: **every request is authorized fresh**.
- A session is **bound to the principal(s) that opened it**. A different token presenting the same session ID is refused.
- DELETE on the endpoint ends a session.
— **Reversibility:** costly — session storage and the multi-instance fan-out shape the module's internals.
- **D-26:** **`list_changed` is pushed.** These events fan out over Redis pub/sub to the instance holding the session, which sends `notifications/tools/list_changed` (and `prompts/list_changed`):
- frontend user or group permission changes;
- backend permission changes;
- link set or removed;
- grant or token revoked.
Every request still re-checks (D-08), so the push only shortens how long a client shows a stale list.
- **D-27:** **Sessions use a sliding idle TTL** (configurable, for example 30 min) in Redis. Revoking the grant or token, deleting the user or removing the link ends or downgrades the session.
- **D-28:** **The capability is designed for tools, prompts and resources; 19.7 ships tools and prompts.** A prompt is a template with arguments, filtered by the same guard and permissions and prefixed like tools (D-12). Resources (URI template → GET route) are deferred, but the `pact` capability's shape leaves room for them without a breaking change. — **Reversibility:** costly — the pact capability is a published framework API.
- **D-29:** **Module name `pier`**, where outside boats (agents) dock to reach the beach. It gets a README in the standard structure with a `**Name:**` mnemonic and a row in the root `README.md` table.
- **D-30:** **Off by default.** With pier disabled nothing is mounted and nothing changes, the same pattern as `http.api_docs.enabled` mounting `/api-docs` (`modules/surf/apidocs.go`).
#### Quality gates (from the roadmap)
- **D-31:** The phase gets a **security review**: link proof, the D-17 context marker, session binding, scope ceilings and superuser reach. **Unit tests are the last plan.** Commit order follows the existing pattern:
1. framework release (pier, the cabana seam, the bouncer marker);
2. sm-user-plugin;
3. sm-oauthserver-plugin (`backend` scope, consent line);
4. sm-journal-plugin (tools, marker);
5. app pointer bumps as separate commits.
fonoteka.go and sm-bm-app must still build and pass at the bumped pins. Push, tag and repo work need the user's go-ahead.
### Claude's Discretion
- Exact Go identifiers: the pact capability and tool/prompt struct names, the link service, the context marker, the table name and the permission code.
- Config key spellings (enable flag, endpoint path with default `/mcp`, the personal-token flag, the session TTL).
- The link-request token format and TTL, within "one-shot, signed, short-lived, bound to the admin".
- The cache bound for the per-request link lookup.
- Which Journal and user tools ship beyond the minimum that proves both guards.
- How the cabana seam is shaped (contribution interface vs extending `AdminController`), as long as it is generic and additive.
### Deferred Ideas (OUT OF SCOPE)
- **MCP resources** (URI template → GET route, guard-filtered) and `resources/subscribe`. The capability is shaped for them (D-28), so a follow-up phase adds them.
- **Generated admin CRUD tools from `fields.yaml`.** Ruled out in the design note: agents use plugin management routes.
#### Reviewed Todos (not folded)
The other `todo.match-phase` hits matched keywords only and are unrelated:
- `journal-author-profile-fields`
- `user-plugin-commands-open-database` (already folded into 19.5)
- `translate-messages-as-nuxt-i18n-json-editor`
- `reload-open-apps-when-a-new-version-is-deployed`
- `uploads-keep-client-extension-stored-xss`
</user_constraints>
<phase_requirements>
## Phase Requirements
No requirement IDs are mapped to this phase (ROADMAP: "Requirements: TBD"). The planner maps the four roadmap success criteria and D-01..D-31 instead:
| ID | Description | Research Support |
|----|-------------|------------------|
| SC-1 | Module enabled in config serves an MCP endpoint; disabled (default) mounts nothing | Pattern 1 (config-gated mount like `mountAPIDocs`), Pitfall 1 (surf buffers responses, so the mount needs a streaming route kind), Open Question 2 (build tag vs always linked) |
| SC-2 | Plugin declares tools through an optional `pact` capability (name, description, input struct → JSON schema, guard, route); none declared → none exposed | Pattern 2 (pact capability shape, SDK-free), Pattern 4 (schema reflection with jsonschema-go `ForType`), D-12 prefixing |
| SC-3 | `tools/list` filtered per principal; a call runs through the route's own middleware | Pattern 3 (surf route handler lookup + in-process dispatch), Pattern 5 (guard providers and requirement parsing), Pitfall 3 (frontend permission semantics differ between `user.permission` and `MergedPermissions`) |
| SC-4 | One OAuth sign-in as frontend user; verified link → backend tools as that admin; removing link/grant removes them; security review; SDK decision note; unit tests last | Pattern 6 (token verification: `oas_at_` verifier does not exist yet), Pattern 7 (link proof via fresh frontend JWT), Pattern 8 (sessions in Redis with rehydration), Security Domain |
</phase_requirements>
## Project Constraints (from CLAUDE.md)
- **Standard library first.** A new dependency needs a research doc or a phase/decision note naming it (`.planning/research/STACK.md` has the decided set). The go-sdk and jsonschema-go need the decision note **before** code lands (D-24).
- **YAML via `goccy/go-yaml` only**, never `gopkg.in/yaml.v3`.
- **Core plugins (user, blog/journal, pages, payment) — no breaking changes.** Every change in sm-user-plugin, sm-journal-plugin and sm-oauthserver-plugin must be additive (existing payloads, routes, tables, codes unchanged).
- **Framework code never names a consuming application** (READMEs, docs, examples use neutral names such as `blog`/`acme`). pier must not name sm-user-plugin, Journal or MCP clients in code or docs; guard providers come in through `app.Lookup`.
- **One Go module**; a new module gets a beach name (`pier`, D-29), a README in the standard structure (H1, one-sentence summary, import line, `**Name:**`, Overview, Features, Usage, API reference, Configuration, Dependencies, Testing) and a row in root `README.md` reusing the summary sentence.
- **Docs rule:** a change to a module's exported API, config keys, CLI commands or dependencies updates that module's `README.md` and the affected `docs/` pages in the same commit. Every identifier in a README/docs page must exist (docs checker `summer docs:build --check`); config keys are reviewed by hand. Expect new `docs/services/mcp.md` (or similar) and updates to `docs/services/oauth-server.md`, `docs/services/authentication.md`, `docs/plugins/registration.md`, `docs/backend/users-and-permissions.md`.
- **Tests:** `go vet ./...` and `go test ./...` green at every commit; Docker tests skip under `-short`; a `TestMain` starting containers calls `flag.Parse()` then checks `testing.Short()`.
- **Admin SPA:** a change in `admin/` (the My Profile seam) requires rebuilding and committing `modules/boardwalk/dist` and passing `scripts/check-admin-dist.sh`; swag annotation changes need `scripts/check-admin-openapi.sh` (cabana `admin_openapi.go` → `admin/openapi/admin.json` → `admin/src/api/schema.d.ts`).
- **MSD rules:** lean planning (few large plans; present plan count + one-line scopes and wait for confirmation); unit tests are the last plan; STATE.md only through `msd_run query state.*`; g15office sync.
- **Commits:** no co-author tags; one logical change per commit; planning docs and code in separate commits; stage files by explicit path; several sessions work concurrently (19.6 and 19.8 are in flight — do not touch their uncommitted files). No push/tag/deploy without go-ahead; every pushed `vX.Y.Z` framework tag gets a changelog post the same day.
- **Shared plugin repos:** commit and push the plugin first, then the pointer bump in the app as a separate commit (`.planning/notes/core-plugins-own-repos.md`).
- **User memory:** never add co-author tags; core plugins must not break.
## Summary
The phase is buildable with the official Go SDK, but three facts discovered this session reshape the plan relative to what CONTEXT assumes:
1. **The go-sdk (v1.8.0, 2026-09-04) holds stateful sessions only in process memory** — `StreamableHTTPHandler.sessions map[string]*sessionInfo` with no pluggable store, and an unknown `Mcp-Session-Id` answers `404 session not found` [VERIFIED: go-sdk@v1.8.0/mcp/streamable.go:47-55, 558-566 — `sessions map[string]*sessionInfo // keyed by session ID`, `http.Error(w, "session not found", http.StatusNotFound)`]. D-25's Redis-shared sessions are still achievable: pier writes a thin session router over the SDK's **exported** `StreamableServerTransport{SessionID}` and `Server.Connect(ctx, t, &ServerSessionOptions{State})`, persisting the JSON-serialisable `ServerSessionState` in Redis and **rehydrating** a session on whichever instance receives the request. A probe built this session proved it: a session initialised on instance A served its next `tools/call` on instance B after rehydration [VERIFIED: local probe, output "rehydrated sess-A on B … call2: map[id:9 served_by:B]"]. Per-session `*mcp.Server` instances give per-principal tool lists and targeted `notifications/tools/list_changed` (the SDK debounces and sends it to that server's sessions when tools are added/removed) [VERIFIED: local probe "list_changed received; tools now: 1"].
2. **The MCP spec moved to a sessionless protocol.** Spec 2026-07-28 removed `initialize` and the `Mcp-Session-Id` session [CITED: blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/]. The SDK serves 2026-07-28 **only** in `Stateless` mode; a stateful server rejects such requests with `CodeUnsupportedProtocolVersion` listing the legacy versions, and SDK clients fall back to `initialize` at 2025-11-25 (probe: "stateful negotiated: 2025-11-25 session: true"). D-25 therefore pins pier to protocol ≤ 2025-11-25. This works today, but it is a forward-compatibility risk the user should confirm (Open Question 1).
3. **Several pieces CONTEXT treats as existing do not exist yet:** (a) nothing verifies `oas_at_` MCP access tokens at a resource — the oauthserver README says "Verifying MCP access tokens at a resource arrives with the MCP module, not here"; (b) the MCP consent page is "the application's" (wristband redirects to `{issuer}/connect?request=`) and neither sm-oauthserver-plugin nor vue-demo-app ships one; (c) there is **no backend Users admin screen** in cabana (admins are CLI-managed), so D-04's "record action on the backend Users screen" has no host; (d) wristband's MCP authorize allow-list of scopes is a hard-coded map, so adding `backend` needs a framework change, not only a plugin option; (e) the user plugin has **no** `user.api_token`-guarded route for "own profile" (`/fetch` is JWT-only), so D-18's frontend tool needs a new additive route; (f) surf's per-route recovery **buffers the whole response and makes `Flush` a no-op**, so an SSE endpoint cannot be mounted through the normal route wrapper.
**Primary recommendation:** Build pier as a framework module that (a) gets a compiled per-route handler lookup from surf (additive `Router` method plus `SetPathValue`-based dispatch, mirroring `overlap.go`), (b) mounts `/mcp` through a new non-buffering streaming route kind, (c) authenticates every HTTP request itself through guard/verifier providers published by plugins (`oas_at_` verifier in sm-oauthserver-plugin, user token resolver in sm-user-plugin, cabana built in), (d) runs a Redis-backed rehydrating session router over the SDK transport with one `*mcp.Server` per session, and (e) passes identity to routes through a context-only dispatch marker honoured in `bouncer.Registry.Middleware` and `cabana.ResolveBackend` — which covers `user.api_token` and Journal's `requireBackendPrincipal` with no change to their code (Journal needs only a one-line change in `optionalBackendPrincipal`).
## Architectural Responsibility Map
| Capability | Primary Tier | Secondary Tier | Rationale |
|------------|-------------|----------------|-----------|
| MCP transport (POST/GET/DELETE `/mcp`, SSE, sessions) | API / Backend (pier) | Database/Storage (Redis session registry) | Protocol lives in the binary; Redis only stores session state and fans out events |
| Bearer verification per request | API / Backend (pier, via published verifiers) | — | `oas_at_` verifier in sm-oauthserver-plugin, user personal tokens in sm-user-plugin, `g15_` in cabana; pier never parses another plugin's tables |
| Tool declaration (name, schema, guard, route) | API / Backend (plugins via `pact`) | — | Compiled Go declarations, SDK-free contract |
| Tool execution | API / Backend (surf in-process dispatch) | — | Runs the route's own middleware chain (D-10) |
| Per-principal filtering | API / Backend (guard providers) | Database (permission reads) | The same code that enforces the route answers "may call" |
| OAuth authorize/consent/token | API / Backend (wristband + sm-oauthserver-plugin) | Browser (consent page) | Consent UI is the application's SPA page or a plugin-rendered page |
| Link proof round-trip | API / Backend (sm-user-plugin) | Browser (site sign-in page, admin SPA My Profile) | Only the user plugin can verify a frontend sign-in (D-03) |
| My Profile contribution seam | Frontend admin SPA (boardwalk/admin) | API (cabana account endpoints) | Generic seam, rendered by the SPA from JSON |
| Session store and event fan-out | Database / Storage (Redis via cooler) | API | Shared state across instances (D-25/D-26) |
## Standard Stack
### Core
| Library | Version | Purpose | Why Standard |
|---------|---------|---------|--------------|
| `github.com/modelcontextprotocol/go-sdk` (packages `mcp`, `auth`, `oauthex`) | v1.8.0 (2026-09-04) [VERIFIED: proxy.golang.org `@latest` → `{"Version":"v1.8.0","Time":"2026-09-04T08:08:52Z"}`] | MCP server, streamable HTTP transport, PRM types, test client | Locked by D-24; official SDK; README: "official Go software development kit (SDK) for the Model Context Protocol"; supports specs 2026-07-28, 2025-11-25, 2025-06-18, 2025-03-26, 2024-11-05 [VERIFIED: go-sdk@v1.8.0/README.md] |
| `github.com/google/jsonschema-go` (package `jsonschema`) | v0.4.3 (2026-04-17) [VERIFIED: proxy.golang.org info] | Reflect tool input/output schemas from Go structs; validate arguments | Already the SDK's own schema library; `ForType(reflect.Type, *ForOptions)` works on a type known only at runtime |
| `github.com/redis/go-redis/v9` | v9.23.0 (already in framework go.mod) [VERIFIED: summercms.go/go.mod:33] | Session registry, sliding TTL, pub/sub fan-out | Already decided in 19.3 (`cooler`) |
| Go stdlib `net/http`, `crypto/rand` (`rand.Text`), `net/http/httptest` (ResponseRecorder for in-process dispatch) | Go 1.27.2 installed [VERIFIED: `go version`] | Dispatch, session IDs | Stdlib first |
Go version requirement: go-sdk `go 1.25.0`, jsonschema-go `go 1.23.0` [VERIFIED: go.mod of each]; the framework is on `go 1.27.0`, so no toolchain change.
### Supporting
| Library | Version | Purpose | When to Use |
|---------|---------|---------|-------------|
| `github.com/alicebob/miniredis/v2` | v2.40.0 (already in go.mod) | Redis pub/sub and TTL in Docker-free tests | Session store and fan-out unit tests |
| `github.com/testcontainers/testcontainers-go` + `modules/postgres` | v0.44.0 (already) | Postgres-backed link/permission/token tests | Docker suites (skipped under `-short`) |
| go-sdk `mcp.Client` + `StreamableClientTransport` | v1.8.0 | End-to-end tests: connect, `ListTools`, `CallTool`, `ToolListChangedHandler` | Every pier integration test; its `HTTPClient` field carries the bearer |
### Transitive modules linked by importing `mcp` + `auth` + `jsonschema`
[VERIFIED: `go list -deps` on a probe module this session]
`github.com/google/jsonschema-go v0.4.3`, `github.com/segmentio/asm v1.1.3`, `github.com/segmentio/encoding v0.5.4`, `github.com/yosida95/uritemplate/v3 v3.0.2`, `golang.org/x/oauth2 v0.35.0`, `golang.org/x/sync v0.20.0`, `golang.org/x/sys v0.41.0`, `golang.org/x/time v0.15.0`. The framework already requires newer `x/sync v0.23.0` and `x/sys v0.48.0`, so MVS keeps those; **new to the graph:** go-sdk, jsonschema-go, segmentio/asm, segmentio/encoding, uritemplate, x/oauth2, x/time. `golang-jwt/jwt/v5` and `x/tools` appear in go-sdk's go.mod but are **not linked** by these packages.
**Binary size:** a stripped probe using `mcp`+`auth`+`jsonschema` is 8,835,335 bytes vs 5,832,967 for a bare `net/http` program — about **+2.9 MiB** [VERIFIED: local build probe]. The real increment in an app that already links x/sys etc. will be somewhat smaller. This matters for Open Question 2.
**License:** go-sdk LICENSE states a transition from MIT to Apache-2.0 ("All new code … licensed under Apache-2.0 … Contributions … not yet granted explicit permission to relicense remain licensed under the MIT License") [VERIFIED: go-sdk@v1.8.0/LICENSE]; jsonschema-go is MIT [VERIFIED: jsonschema-go@v0.4.3/LICENSE]. Both permissive.
### Alternatives Considered
| Instead of | Could Use | Tradeoff |
|------------|-----------|----------|
| SDK `StreamableHTTPHandler` as-is | Own session router over `StreamableServerTransport` | The handler cannot share sessions across instances (in-memory map, 404 on miss). The own router (~250 lines) is required for D-25 rehydration; the transport, JSON-RPC, SSE and protocol negotiation stay the SDK's |
| One shared `*mcp.Server` + receiving middleware filter | One `*mcp.Server` per session | The SDK has no exported "notify this session of list_changed"; per-session servers get it for free via `AddTool`/`RemoveTools` (probe verified). Middleware filtering is still added for the per-request re-check (D-08) |
| Stateful only (D-25) | Hybrid: stateless SDK handler for 2026-07-28 requests, stateful router for ≤2025-11-25 | Forward-compatible with the current spec; needs user confirmation because D-25 is locked (Open Question 1) |
| `mark3labs/mcp-go` | — | Not considered: D-24 locks the official SDK |
**Installation (framework go.mod only; plugins must not require the SDK):**
```bash
go get github.com/modelcontextprotocol/go-sdk@v1.8.0 github.com/google/jsonschema-go@v0.4.3
```
**Version verification:** `go list -m -versions github.com/modelcontextprotocol/go-sdk` lists `… v1.7.0 v1.8.0-pre.1 v1.8.0-pre.2 v1.8.0`; no v1.9 pre-release exists yet [VERIFIED: go proxy, 2026-10-21]. The SDK notes the `allowsessionsinstateless` compatibility knob "will be removed in the 1.9.0 version" — irrelevant to a stateful server but a sign 1.9 is coming; re-check `@latest` before `go get` if executing after ~2026-11-20.
## Package Legitimacy Audit
The `package-legitimacy check` seam supports only npm/pypi/crates ("Usage: msd-tools package-legitimacy check --ecosystem <npm|pypi|crates>"), so Go modules cannot get an `OK` verdict from it. Per the provenance rule the package names are tagged `[ASSUMED]` and need a `checkpoint:human-verify` before `go get`, as 19.6 did for its Go modules.
| Package | Registry | Age | Downloads | Source Repo | Verdict | Disposition |
|---------|----------|-----|-----------|-------------|---------|-------------|
| `github.com/modelcontextprotocol/go-sdk` [ASSUMED] | Go proxy (sum.golang.org verified by `go mod download`) | v1.8.0 published 2026-09-04; v1.x line since 2025 | n/a (Go) | github.com/modelcontextprotocol/go-sdk (proxy Origin URL, hash 3f3b699b) | seam: not supported | Flagged — planner adds `checkpoint:human-verify` before `go get` |
| `github.com/google/jsonschema-go` [ASSUMED] | Go proxy | v0.4.3 published 2026-04-17 | n/a | github.com/google/jsonschema-go (proxy Origin URL, hash 8c4ab4f0) | seam: not supported | Flagged — same checkpoint |
| `segmentio/asm`, `segmentio/encoding`, `yosida95/uritemplate/v3`, `x/oauth2`, `x/time` (transitive) [ASSUMED] | Go proxy | — | n/a | via go-sdk go.mod | seam: not supported | Listed in the decision note; human check covers them; run `govulncheck` (not installed here: `go run golang.org/x/vuln/cmd/govulncheck@latest ./...`) |
**Packages removed due to [SLOP] verdict:** none
**Packages flagged as suspicious [SUS]:** none by the seam (unsupported ecosystem); all Go additions are gated by one human-verify checkpoint.
## Architecture Patterns
### System Architecture Diagram
```
MCP client (editor's agent)
│ POST/GET/DELETE /mcp Authorization: Bearer <oas_at_… | usr_… | g15_…> Mcp-Session-Id
▼
surf mux ──(streaming route kind: no buffering, throttle:mcp, Origin check)──► pier HTTP layer
│
┌──────────────────────────────────────────────────────────────────────────────┤
│ 1. verify bearer (EVERY request) │
│ oas_at_ ─► oauthserver verifier (token row, client kind=mcp, not revoked) │
│ usr_… ─► user plugin resolver (only if allow_personal_tokens) │
│ g15_… ─► cabana token resolver (only if allow_personal_tokens) │
│ fail ──► 401 + WWW-Authenticate resource_metadata, scope │
│ 2. build Identity: frontend principal (+creds), backend principal if │
│ scope∋backend AND user plugin LinkedBackendUser(uid) (D-08, per request) │
│ 3. session router │
│ no id ─► new id (crypto/rand.Text) ─► per-session *mcp.Server built from │
│ Identity's allowed tools/prompts ─► Redis SET pier:s:<id>{bind, │
│ state, ttl} │
│ id ─► Redis GET ─► binding == Identity.binding ? else 403 │
│ local transport? else REHYDRATE (Connect with stored State) │
│ EXPIRE (sliding TTL) │
│ DELETE ─► Redis DEL + PUBLISH ended ─► local Close │
└──────────────────────────────────────────────────────────────────────────────┘
│ JSON-RPC (SDK transport/session)
▼
receiving middleware: tools/list & prompts/list re-filtered for THIS request's Identity,
tools/call refused if not allowed (scope ceiling D-20, guard, permissions)
│ tools/call
▼
tool handler: validate args (JSON schema) ─► split path/query/body ─► build *http.Request
(no Authorization/Cookie, RemoteAddr/XFF/Accept-Language copied, SetPathValue, Pattern)
ctx = bouncer dispatch marker{guard names, principal, credential}
│
▼ surf compiled route handler (constraints, body limit, named middleware,
throttle, user.api_token/user.scope/user.permission, recovery) — D-10
▼
ResponseRecorder ─► 2xx: text + structuredContent ; 4xx/5xx: isError + route body (D-13)
Event sources (user plugin perms/link/token revoke, oauthserver grant revoke, cabana token revoke)
─► festival event ─► pier listener ─► Redis PUBLISH pier:events
─► every instance: sessions bound to that subject recompute tool set (Add/RemoveTools
→ SDK sends notifications/tools/list_changed) or Close on revocation (D-26/D-27)
```
### Recommended Project Structure
```
modules/pier/ # framework module; the only package importing go-sdk
├── README.md # standard structure, **Name:** mnemonic
├── pier.go # Config, Mount(r surf-adapter, app, plugins), boot validation
├── catalog.go # compile pact declarations → tools/prompts, prefixing (D-12), schemas
├── dispatch.go # args → *http.Request, ResponseRecorder → CallToolResult (D-11, D-13)
├── identity.go # bearer classification, verifiers, Identity, binding key
├── guards.go # guard provider registry ("backend" built in, others via app.Lookup)
├── session.go # rehydrating session router over StreamableServerTransport
├── store_redis.go / store_memory.go # session registry + pub/sub (memory for single instance/tests)
├── prm.go # RFC 9728 protected resource metadata + WWW-Authenticate
└── events.go # festival listeners → fan-out
modules/pact/capabilities.go # + HasMCP (tools, prompts, room for resources), HasFrontendPermissions
modules/bouncer/dispatch.go # + context-only dispatch marker, honoured in Registry.Middleware
modules/surf/ # + RouteHandler lookup after compile, streaming route kind, pier mount (tagged or not)
modules/cabana/ # + ResolveBackend honours marker, account contribution seam, BackendPrincipal(ctx, app, id), token resolver without method filter
modules/wristband/authorize.go # + MCP allowed scopes from Options.ScopesSupported (default unchanged)
modules/cooler/ # + expose/publish the Redis client when pier selects redis
admin/src/views/AccountView.vue # + generic account sections/actions component; rebuild boardwalk/dist
```
### Pattern 1: Config-gated mount, after all routes exist
**What:** pier mounts like `mountAPIDocs` — nothing when disabled. Unlike api-docs it must validate tool → route bindings, so it runs **after** every plugin route and after `cabana.Activate` in `BuildRouter` (routes 624-654), before the final `r.wrap` validation loop.
**Existing code:** `if err := mountAPIDocs(r, app, plugins)` at `modules/surf/router.go:635`, `admin, err := cabana.Activate(app, plugins)` at :638 [VERIFIED: modules/surf/router.go:635-638]. `route:list` also calls `BuildRouter`, so a bad binding fails `route:list` too (consistent with "boot fails", D-10).
**Import direction:** surf imports cabana, cooler and bouncer already. If surf imports pier, pier must not import surf: pier defines a small interface (`Routes() []RouteInfo`, `Handler(method, pattern) (http.Handler, bool)`), surf passes an adapter.
### Pattern 2: SDK-free pact capability (D-28 shape)
**What:** plugins import `pact` only; pact must not import the SDK or jsonschema-go (otherwise every app with the user plugin links the SDK). Use one method returning a struct so `Resources` can be added later as a field (additive):
```go
// pact (sketch; identifiers are the planner's call)
type MCP struct {
Tools []MCPTool
Prompts []MCPPrompt
// Resources []MCPResource — added in a later phase, non-breaking
}
type HasMCP interface{ MCP() MCP }
type MCPTool struct {
Name, Title, Description string
Guard string // "user" | "backend" — names a published guard provider
Method, Path string // the plugin's OWN route, e.g. POST /_journal/api/v1/posts
Input any // zero value of the input struct (reflected with jsonschema.ForType)
Output any // optional; nil → no outputSchema
Permissions []string // D-16: list filter on top of route-derived requirements
ReadOnly, Destructive, Idempotent, OpenWorld *bool // D-14 overrides; nil → derived from Method
}
type HasFrontendPermissions interface{ FrontendPermissions() []FrontendPermission } // D-09: Code, Label, Tab, Comment
```
Existing analogs: `pact.Permission{Code, Tab, Label, Roles}` and `HasPermissions` [VERIFIED: modules/pact/capabilities.go:512-522 — `type Permission struct { Code string; Tab string; Label string; Roles []string }`, `type HasPermissions interface { Permissions() []Permission }`].
### Pattern 3: In-process dispatch through the compiled route handler
**What:** surf already compiles one wrapped handler per route (`handlers[i] = h` inside `compile()`, `modules/surf/router.go:354-364`) but keeps them local. Add an additive accessor that records them (keyed by `method + " " + path`) and expose it after compile. Dispatch calls that handler **directly**, not the mux, after setting path values exactly as `overlap.go` does (`r.SetPathValue(s.param, vals[i])` … `r.Pattern = m.pattern` [VERIFIED: modules/surf/overlap.go:415-420]).
**Why not the mux:** dispatching a concrete URL through ServeMux can match a *different*, more specific route (a `{id}` value `export` would hit `/posts/export`). Calling the bound route's handler makes the binding exact; `constrain` inside that handler still enforces `Where` constraints.
**What the wrapped handler contains** (`wrap`, :408-491): constraints → org slot → named middleware (right to left) → body limit → locale → `recoverJSON`/`recoverBare` → CORS profile → `recordPattern`. Calling it runs throttles, `user.api_token`/`user.scope`/`user.permission`, and the handler — D-10's "same code path as HTTP". The outer access log is not on that path, so pier logs tool calls itself (D-23).
**Request construction rules:** method/path from the binding; values for single-segment params must not contain `/`; query from `query:` fields; body = the agent's JSON object minus path/query keys (forward raw, see Pitfall 6); copy `RemoteAddr`, `X-Forwarded-For`/`X-Real-IP`, `Accept-Language`; set `Content-Type`/`Accept: application/json`; **drop `Authorization` and `Cookie`** (no token passthrough); attach the dispatch marker to the context; a per-call timeout.
### Pattern 4: Schema reflection and validation
`jsonschema.ForType(reflect.TypeOf(tool.Input), nil)` builds the input schema. Properties come from the JSON name; fields without `omitempty`/`omitzero` become `required`; the `jsonschema` tag is the description; structs get `additionalProperties: false` [VERIFIED: jsonschema-go@v0.4.3/jsonschema/infer.go:56-82, 329-343]. The SDK's low-level `Server.AddTool(t, h)` panics unless the input schema has `"type": "object"` and leaves validation to the caller ("Unmarshaling the arguments and validating them against the input schema are the caller's responsibility") [VERIFIED: go-sdk@v1.8.0/mcp/server.go:295-345]. pier therefore: reflects once at boot (fail boot on error), resolves the schema once, validates every call's arguments, then splits by the `path`/`query` struct tags.
### Pattern 5: Guard providers keep the framework neutral
pier must not hard-code `user.api_token`/`user.scope:`/`user.permission:` (sm-user-plugin names). Recommended shape: a framework interface each guard owner publishes via `app.Publish`:
```go
// framework, SDK-free package (pact or a small pier contract package) — names are the planner's call
type MCPGuard interface {
Name() string // "user", "backend"
TokenGuarded(routeMiddleware []string) bool // D-15 check at boot
Requirements(routeMiddleware []string) (any, error) // parses its own names (user.scope:read, user.permission:x)
Allows(ctx context.Context, id Identity, req any, toolPerms []string) bool // the SAME logic the route enforces
DispatchGuardNames() []string // bouncer registry names to honour, e.g. ["api_token"]
}
```
cabana's "backend" guard is built into pier (framework may use cabana). The user plugin publishes "user". Existing wiring: the user plugin registers bouncer guards `"jwt"` and `"api_token"` and exposes them as middleware `jwt.auth`/`user.api_token`; factories `user.scope`/`user.permission` [VERIFIED: fonoteka.go/plugins/golem15/user/plugin.go:100,108,244,247,254-255 — `reg.Register(p.ID(), "jwt", …)`, `reg.Register(p.ID(), "api_token", …)`, `out["jwt.auth"] = mw`, `out["user.api_token"] = mw`, `"user.scope": usermiddleware.Scope`, `"user.permission": usermiddleware.Permission`]. cabana registers bouncer guard `"backend"` [VERIFIED: modules/cabana/http.go:164 — `guards.Register("summercms.cabana", "backend", guard)`].
### Pattern 6: The D-17 marker — where it must be honoured
- **bouncer** `Registry.Middleware` calls `authenticate(ng.g, r)` and, for a guard with `UnauthorizedWriter`, writes 401 on failure [VERIFIED: modules/bouncer/registry.go:73-104]. Add before line 83: if the context carries a dispatch marker that lists this guard name, use its principal/credential and skip header parsing. This single change covers `user.api_token` (the bouncer `api_token` guard) and `jwt.auth` — **no sm-user-plugin code change is needed for the marker itself** (D-17's "user.api_token honours the marker" is satisfied in bouncer). Decide whether `jwt.auth` may honour it; D-15 says JWT-session routes are not tool targets, so honour only `api_token` for the user guard.
- **cabana** `ResolveBackend` returns `ErrUnauthenticated` immediately when there is no bearer (`if app == nil || r == nil || authorizationBearer(r) == ""` [VERIFIED: modules/cabana/token_guard.go:199-200]). Put the marker check before that line. Journal's `requireBackendPrincipal` calls `cabana.ResolveBackend(app, r, journalCover)` [VERIFIED: sm-journal-plugin/controllers/api/auth.go:85], so it honours the marker with **no Journal change**.
- **Journal** `optionalBackendPrincipal` short-circuits on `if r == nil || !hasBearer(r)` [VERIFIED: sm-journal-plugin/controllers/api/auth.go:70], so the list/show tools (public GET routes) would run anonymous (published posts only). Additive one-line fix: drop the `hasBearer` short-circuit (ResolveBackend already returns ErrUnauthenticated for a missing bearer, so anonymous behaviour is identical) or check the marker explicitly.
- **Credential for `user.scope`:** `Scope` reads `bouncer.Credential` as `*models.APIToken` and calls `token.HasScope(scope)` [VERIFIED: fonoteka.go/plugins/golem15/user/middleware/api_token.go:90-101]. An OAuth session has no `user_api_tokens` row, so the user guard provider must build an **in-memory** `*models.APIToken{UserID, Scopes: grant scopes}` (never persisted) for the marker; user token scopes are `AllowedAPITokenScopes = []string{"read", "write", "ai"}` [VERIFIED: fonoteka.go/plugins/golem15/user/classes/api_tokens.go:23], the same vocabulary as MCP OAuth scopes. Handlers that read `credential.ID` must tolerate ID 0 (Pitfall 9).
- **Unforgeability:** context values cannot be set from HTTP; the marker type lives in bouncer with an unexported key. Any compiled plugin could still call the setter, so restrict by convention plus a check-script grep that only `modules/pier` calls it.
### Pattern 7: Link proof through a fresh frontend session (D-05)
The 19.6 login gate (`Login` consults gates after `CheckAndRecordLogin`, before `mintFor`; 19.6 D-02) and sociallogin's `oauth-complete` both end in a frontend JWT. The proof therefore does not need its own login flow:
1. Admin (interactive cabana session; `interactiveOnly` refuses `g15_` tokens) clicks "Link frontend account" → sm-user-plugin creates a link request row (random 32-byte code, stored hashed, `backend_user_id`, `expires_at` ~10 min, `used_at`) and returns a site URL with the code.
2. The site page shows "Link this account to admin <name>?" (public `GET` showing only the admin display name and expiry), asks the user to sign in **fresh** (normal login page, so password, social login and 2FA gates all apply).
3. `POST …/backend-link/confirm` with `jwt.auth` + the code: row-locked, one-shot, not expired, **JWT `iat` after the request's `created_at`** (forces a fresh sign-in rather than a stale browser session), uniqueness on both sides (D-06) → save link, fire event.
19.6 adds `golem15.user.frontend_url` (19.6-01-PLAN artifacts) — reuse it for the site URL. The site page is app-specific (vue-demo-app has only `contact, faq, index, login, register, roadmap, subscriptions, p/, presentations/, quizzes/` pages [VERIFIED: ls sm-demo-app/vue-demo-app/app/pages]); add it there.
### Pattern 8: Redis-backed rehydrating sessions (D-25..D-27)
- New session: `crypto/rand.Text()` id; connect a per-session `*mcp.Server`; after `initialize`, store `{binding, ServerSessionState{InitializeParams, InitializedParams: new(…), LogLevel: "info"}, created}` under `<prefix>pier:s:<id>` with `PX ttl`. `ServerSessionState` is JSON-tagged for this purpose [VERIFIED: go-sdk@v1.8.0/mcp/session.go:18-36].
- Existing id, local miss: load state, `server.Connect(ctx, &mcp.StreamableServerTransport{SessionID: id}, &mcp.ServerSessionOptions{State: st})`, then `t.ServeHTTP` (probe-verified). `ServerSessionOptions.onClose` is unexported, so track closure with a goroutine on `ss.Wait()`.
- Binding: refuse with 403 when the request's binding key differs. **Binding key must survive access-token refresh** (1 h access TTL, rotation): bind to `(credential family, frontend user id)` — e.g. `oauth:<client_id>:<user_id>`, `pat:user:<token_id>`, `pat:backend:<token_id>` — not the token hash (Open Question 5).
- Every request: `PEXPIRE` (sliding TTL). Missing key → 404 (spec: client re-initialises).
- DELETE: `DEL` + `PUBLISH pier:events {ended:id}` → every instance closes its local copy.
- Fan-out: `PUBLISH` on events; each instance iterates its local sessions bound to the subject and either recomputes the allowed tool set (diff → `AddTool`/`RemoveTools` → SDK sends `notifications/tools/list_changed`, debounced 10 ms [VERIFIED: go-sdk@v1.8.0/mcp/server.go:740-770]) or closes the session on revocation.
- Memory driver for single instance and tests (same interface), mirroring cooler's memory/redis split. cooler opens its Redis client only when `cache.driver` or `http.rate_limit.store` selects redis and keeps it unexported (`Handle.client`) [VERIFIED: modules/cooler/cooler.go:109-116, 184-245]; add an additive selector/accessor for pier.
### Pattern 9: Protected resource metadata (required by the MCP auth spec)
"MCP servers **MUST** implement OAuth 2.0 Protected Resource Metadata (RFC9728)"; serve it at `/.well-known/oauth-protected-resource/<mcp path>` and/or root, and include `resource_metadata` (and `scope`) in `WWW-Authenticate` on 401 [CITED: modelcontextprotocol.io/specification/2025-11-25/basic/authorization]. Nothing in the framework or oauthserver serves PRM today (grep found only fonoteka-mcp's Node server). The SDK provides `auth.ProtectedResourceMetadataHandler(*oauthex.ProtectedResourceMetadata)`; mount it through surf at both paths. `resource` must equal `golem15.oauthserver.mcp.resource`, because wristband refuses a `resource` parameter that differs from `Options.Resource` and clients MUST send `resource` ("MCP clients MUST send this parameter regardless") (Pitfall 8).
### Anti-Patterns to Avoid
- **Forwarding the MCP bearer to the route.** Token passthrough is forbidden ("The MCP server MUST NOT pass through the token it received") and would also make Journal try to parse an `oas_at_` token as a backend JWT. Identity goes through the marker only.
- **Dispatching through the mux by concrete URL** (route confusion, see Pattern 3).
- **Mounting `/mcp` as a normal surf route** (buffered, see Pitfall 1).
- **pier parsing user-plugin middleware names or reading user-plugin tables** (framework must stay neutral; use guard providers).
- **Putting the SDK in `pact`** or any package plugins import.
- **Imperative or dynamic tool descriptions.** fonoteka-mcp keeps descriptions "STATIC factual `verb_noun` one-liners with NO imperative second-person text (auto-injected tool text is a prompt-injection surface)" and sets title plus explicit read/destructive/open-world hints because "both catalogs reject a submission that is missing any of those annotations" [VERIFIED: /media/nvme/dev/golem15/fonoteka/fonoteka-mcp/src/server.ts:5-17].
## Don't Hand-Roll
| Problem | Don't Build | Use Instead | Why |
|---------|-------------|-------------|-----|
| JSON-RPC framing, SSE event streams, protocol negotiation, `initialize` handshake, 2026-07-28 rejection | Own MCP transport | go-sdk `StreamableServerTransport` + `Server.Connect` | Spec edge cases (batching, priming events, header validation, version negotiation) are already handled; pier only routes sessions |
| JSON schema from Go structs + validation | Reflection code | `jsonschema.ForType` + `Schema.Resolve`/`Validate` | Same library the SDK uses; consistent `required`/`additionalProperties` |
| PRM document and CORS for it | Handwritten JSON | `auth.ProtectedResourceMetadataHandler` + `oauthex.ProtectedResourceMetadata` | RFC 9728 field names |
| Test MCP client | Raw HTTP in tests | `mcp.NewClient` + `StreamableClientTransport{HTTPClient}` | Exercises negotiation fallback, sessions, list_changed |
| Session IDs | Custom generator | `crypto/rand.Text()` (SDK recommendation) | Unguessable IDs |
| Rate limiting `/mcp` | New limiter | surf `throttle:<bucket>` (Redis-backed via cooler when configured) | Existing shared limiter |
| Permission checks for tools/list | Re-implementing permission logic in pier | The guard provider's own check (same function the route middleware uses) | List and call must agree (Pitfall 3) |
| One-time link codes | Signed JWT with replay cache | DB row with hashed random code, row lock, `used_at` | Same pattern as the user plugin's codes (`newCode()` 32 random bytes hex) [VERIFIED: fonoteka.go/plugins/golem15/user/classes/codes.go:46-52] |
**Key insight:** the SDK should own the protocol and pier should own identity and dispatch. The only part of the SDK pier bypasses is `StreamableHTTPHandler`'s private session map, and only because D-25 needs it shared.
## Common Pitfalls
### Pitfall 1: surf buffers every route response — SSE cannot stream
**What goes wrong:** the GET standalone stream and SSE POST responses never reach the client; the GET never returns.
**Why:** `wrap` always ends with `recoverJSON` or `recoverBare` (:479/:481), both of which write into a `bufferedResponse` that commits only after the handler returns, and `func (*bufferedResponse) Flush() {}` is a deliberate no-op ("Route output becomes visible only after the handler returns successfully") [VERIFIED: modules/surf/router.go:479-481, 851-853].
**How to avoid:** add an additive streaming route kind (recovery that does not buffer — after headers are sent a panic can only abort; keep throttle, CORS profile, `recordPattern`). The outer layers (access log `statusRecorder` has Flush/Unwrap; `pathScopedCORS`, error-page fallback and request counter pass the writer through) are fine. No route streams today (no `text/event-stream`/`http.Flusher` use in framework or plugins).
**Warning signs:** client connects, `initialize` works with `JSONResponse` but tool calls hang with SSE.
### Pitfall 2: Protocol 2026-07-28 vs stateful sessions
**What goes wrong:** a client that speaks only 2026-07-28 cannot use a stateful server.
**Why:** "the streamable HTTP transport accepts requests at protocol version `2026-07-28` **only** when `Stateless = true`" [VERIFIED: go-sdk@v1.8.0/docs/protocol.md:352-353]. Stateful servers answer `CodeUnsupportedProtocolVersion` with legacy versions; SDK clients fall back (probe negotiated 2025-11-25).
**How to avoid:** keep D-25 (works with dual-version clients) and ask the user about the hybrid in Open Question 1. Test with the SDK client (which tries `server/discover` first).
### Pitfall 3: Frontend permission semantics differ between the route and `MergedPermissions`
**What goes wrong:** `tools/list` shows a tool the call then refuses, or hides one the call would allow.
**Why:** `user.permission:<code>` checks `cabana.Allows(principal, []string{code})` [VERIFIED: fonoteka.go/plugins/golem15/user/middleware/api_token.go:106-110], and the principal's grants come from `UserPermissionGrants` — **group permissions only** (`PrincipalByID` → `grants, err := UserPermissionGrants(ctx, gdb, row.ID)` [VERIFIED: classes/user_lookup.go:35,54]; it reads `user_groups.permissions` joined through `users_groups` only, `user_lookup.go:65-99`). `MergedPermissions` (groups plus the user's tri-state override) exists but **no route uses it** (grep: only `classes/permissions.go` references it). The design note says tools/list uses `MergedPermissions`.
**How to avoid:** the guard provider answers with the **same** check the route enforces (group grants). Whether routes should move to `MergedPermissions` is a core-plugin behaviour change — Open Question 4. (Backend differs: cabana's `BackendUsers.FindByID` already overlays the admin's own permissions [VERIFIED: modules/cabana/auth.go:39-68].)
### Pitfall 4: No resource-side verifier for `oas_at_` tokens; OIDC tokens share the table
**What goes wrong:** pier cannot authenticate OAuth sessions, or accepts OIDC userinfo tokens as MCP tokens.
**Why:** MCP access tokens are `MCPAccessTokenPrefix = "oas_at_"` rows in `golem15_oauthserver_access_tokens` [VERIFIED: sm-oauthserver-plugin/classes/mcp_store.go:19]; the same table holds OIDC access tokens ("OIDC tokens are only usable at the userinfo endpoint", models/access_token.go). No verifier exists ("Verifying MCP access tokens at a resource arrives with the MCP module, not here" — sm-oauthserver-plugin/README.md:32).
**How to avoid:** sm-oauthserver-plugin publishes a verifier implementing a framework interface: prefix `oas_at_`, hash lookup, not revoked, not expired, client exists with kind `mcp` and not revoked, user active; returns user id, scopes, client id, token id, expiry. Throttle `last_used_at` writes (cabana's `stampThrottle` pattern).
### Pitfall 5: `backend` scope is rejected by wristband's hard-coded allow-list
**What goes wrong:** a client requesting `backend` gets `invalid_scope` even after the plugin adds it to `ScopesSupported`.
**Why:** `var authorizeAllowedScopes = map[string]bool{ "read": true, "write": true, "ai": true, "offline_access": true, }` [VERIFIED: modules/wristband/authorize.go:22-24]; `DefaultOptions().ScopesSupported` is `[]string{"read", "write", "ai", "offline_access"}` [VERIFIED: modules/wristband/server.go:99].
**How to avoid:** framework change in wristband: the MCP flavour's allowed set = `Options.ScopesSupported` when set (default stays the four, PHP parity). Then sm-oauthserver-plugin appends `backend` to `mcpOpts.ScopesSupported` in `NewServers`.
### Pitfall 6: Partial updates and zero values
**What goes wrong:** an update tool sends `"published": false` or `""` for fields the agent never mentioned.
**Why:** Journal's update uses key presence (`if _, has := body["published"]; has {` …) [VERIFIED: sm-journal-plugin/controllers/api/posts_helpers.go:579-580]; re-marshalling a Go struct emits zero values.
**How to avoid:** use the struct only for the schema; forward the agent's **raw** JSON object minus path/query keys as the body. Mark optional fields with `omitempty`/pointers so the schema does not require them.
### Pitfall 7: Method-filtered `g15_` token scopes at a POST endpoint
**What goes wrong:** with `allow_personal_tokens`, a read-only cabana token cannot even open a session.
**Why:** the composite guard filters scopes by the *request* method (`methodFilterScopes(g.reg.tokenScopes, stored, r.Method)` [VERIFIED: modules/cabana/token_guard.go:109]); every MCP request is POST/GET/DELETE to `/mcp`, not the tool's route method.
**How to avoid:** additive cabana API that resolves a `g15_` token without method filtering and computes the `ScopeLimit` per dispatched route method (`ScopeLimit` semantics: "Only a token guard sets Scope, already filtered to scopes whose methods include the request method" [VERIFIED: modules/bouncer/context.go:10-19]).
### Pitfall 8: Resource indicator mismatch breaks sign-in
**What goes wrong:** authorize answers `invalid_target`.
**Why:** wristband refuses a `resource` that differs from `Options.Resource`, which comes from `golem15.oauthserver.mcp.resource` [VERIFIED: sm-oauthserver-plugin/servers.go:123-124]; MCP clients always send `resource` = the canonical MCP URL.
**How to avoid:** one value: pier's resource URL (default `app.url` + path, no trailing slash) must equal `mcp.resource`; pier's PRM `resource` uses the same; a boot warning when they differ.
### Pitfall 9: In-memory credentials and handlers that read `credential.ID`
**Warning:** a synthetic `*models.APIToken` (OAuth session) has `ID == 0` and `CollectionIDs` empty; route handlers that look up the token row by ID or use collection scoping (fonoteka-style) would misbehave. Audit token-guarded tool targets; Journal and the new user tool routes don't use the credential.
### Pitfall 10: Long-lived GET streams outlive revocation
**What goes wrong:** a revoked grant keeps receiving notifications on an already-open SSE stream (authorization happens at stream open).
**How to avoid:** on revocation events close the session on every instance; additionally re-verify the stream's bearer periodically (e.g. every 60 s) and close when invalid. Shutdown: `serve` uses the signal context as `BaseContext`, so request contexts cancel on SIGTERM; streams end.
### Pitfall 11: Name collisions and validity
Short codes collide across vendors (`acme.blog` and `golem15.blog` → `blog_`); prefix collisions must fail boot (D-12 covers within one plugin only). The SDK only *logs* an invalid name (`AddTool: invalid tool name`); names over 128 chars or with invalid runes must fail boot in pier [VERIFIED: go-sdk@v1.8.0/mcp/tool.go:163-186].
### Pitfall 12: `structuredContent` must be an object
A route returning a JSON array or scalar cannot be passed as `structuredContent`. Wrap non-objects (e.g. `{"data": …}`) or omit `structuredContent`, and only declare an `outputSchema` when the 2xx body always matches it (the low-level API leaves output validation to the caller).
### Pitfall 13: Concurrent work on sm-user-plugin
19.6 plans 01-04 modify `controllers/users_admin_controller.go` (`AdminRecordActions` at :665), `controllers/users/config_form.yaml` (`recordActions: [activate, unban, unsuspend]` at :17), `admin_permissions.go`, `plugin.go`, `routes.go`, `controllers/api_controller.go`, `lang/*`, `README.md` and `admin_registration_test.go` in `../fonoteka.go/plugins/golem15/user` (19.6-01-PLAN files_modified). 19.6 is not executed (no SUMMARY files; the user plugin is at `35cf166`, tag v0.1.5). 19.7's user-plugin plan must start after 19.6's user-plugin release, rebase on it, and append to the same lists.
## Code Examples
### Rehydrating a session on another instance (probe-verified)
```go
// Source: local probe against go-sdk v1.8.0 (exported API only)
st := &mcp.ServerSessionState{
InitializeParams: storedInitParams, // from ss.InitializeParams() on the first instance
InitializedParams: new(mcp.InitializedParams),
LogLevel: "info",
}
t := &mcp.StreamableServerTransport{SessionID: id}
if _, err := perSessionServer.Connect(context.Background(), t, &mcp.ServerSessionOptions{State: st}); err != nil {
http.Error(w, "failed connection", http.StatusInternalServerError)
return
}
t.ServeHTTP(w, req) // serves POST/GET for this session on this instance
```
### Targeted list_changed by per-session server (probe-verified)
```go
// Source: local probe; the SDK debounces and notifies only this server's sessions
srv.RemoveTools("journal_create_post") // after the link was removed
// client side receives notifications/tools/list_changed via ClientOptions.ToolListChangedHandler
```
### Per-request identity into the SDK
```go
// Source: go-sdk@v1.8.0/auth/auth.go:96-125 and mcp/streamable.go:1463,1585-1586
// RequireBearerToken is the only setter of the TokenInfo context key; the transport copies it
// into every JSON-RPC request's Extra.TokenInfo, so tool handlers see the CURRENT request's identity.
verify := func(ctx context.Context, token string, r *http.Request) (*auth.TokenInfo, error) {
id, err := identities.Resolve(ctx, token) // oas_at_ / personal tokens; per request (D-25)
if err != nil {
return nil, fmt.Errorf("%w: %v", auth.ErrInvalidToken, err)
}
return &auth.TokenInfo{Scopes: id.Scopes, Expiration: id.ExpiresAt, UserID: id.BindingKey,
Extra: map[string]any{identityKey: id}}, nil
}
h := auth.RequireBearerToken(verify, &auth.RequireBearerTokenOptions{
ResourceMetadataURL: prmURL, Scopes: nil, // pier enforces read/write per tool, not per request
})(sessionRouter)
```
Note: `RequireBearerTokenOptions.AllowMissingExpiration` exists for tokens without expiry (personal tokens with no `expires_at`) [VERIFIED: go-sdk@v1.8.0/auth/auth.go:48-64].
### Annotations from method (D-14)
```go
// SDK type: ToolAnnotations{DestructiveHint *bool; IdempotentHint bool; OpenWorldHint *bool; ReadOnlyHint bool; Title string}
// Source: go-sdk@v1.8.0/mcp/protocol.go:1964-1991
ann := &mcp.ToolAnnotations{Title: tool.Title, ReadOnlyHint: method == http.MethodGet,
IdempotentHint: method == http.MethodPut || method == http.MethodGet}
destructive := method == http.MethodDelete
openWorld := false
ann.DestructiveHint, ann.OpenWorldHint = &destructive, &openWorld // always explicit (catalog requirement)
```
### Journal backend tools (D-18 candidates, existing routes)
Existing routes [VERIFIED: sm-journal-plugin/routes.go:16-34]:
```
GET /_journal/api/v1/posts (public group, throttle:journal-public-api; editor sees drafts)
GET /_journal/api/v1/posts/{slug} (Where slug `[a-z0-9][a-z0-9\-/]*`; numeric slug falls back to id)
GET /_journal/api/v1/categories | /tags
POST /_journal/api/v1/posts (throttle:journal-api; requireBackendPrincipal + access_posts)
PUT /_journal/api/v1/posts/{id} (Where id `[0-9]+`)
DELETE /_journal/api/v1/posts/{id}
POST /_journal/api/v1/media/upload | /posts/{id}/featured-images (multipart — not tool-friendly)
```
Permissions [VERIFIED: sm-journal-plugin/controllers/api/auth.go:26-30]: `permAccessPosts = "golem15.journal.access_posts"`, `permAccessOtherPosts = "golem15.journal.access_other_posts"`, `permAccessPublish = "golem15.journal.access_publish"`, `journalCover = "golem15.journal.*"`. Recommended tools: `journal_list_posts`, `journal_show_post`, `journal_list_categories`, `journal_list_tags` (read), `journal_create_post`, `journal_update_post` (write), `journal_delete_post` (destructive; optional). All declare `Guard: "backend"` and `Permissions: [access_posts]` (D-15/D-16 explicit-guard case). Multipart upload routes are out (JSON args only). The `journal-api` bucket keys by backend principal id when `bouncer.User` holds a backend principal [VERIFIED: sm-journal-plugin/plugin.go:137-146], so marker dispatch is throttled per admin.
### Frontend tool (D-18) needs a new route
`GET /_user/api/v1/fetch` authenticates the JWT inside the handler (`authenticate` → `bouncer.VerifyClaims`) [VERIFIED: fonoteka.go/plugins/golem15/user/controllers/api_controller.go:163-176, 795-825] and is not token-guarded, so it cannot be a target. Add an additive route such as `GET /_user/api/v1/token/me` with `user.api_token`, `user.scope:read` returning the same `{"user": …}` payload via `apiArray`. Other ready frontend targets exist in shared plugins: presentations' manage group (`user.api_token` + `user.scope:read|write`) and quizzes' import (`user.scope:write`, `user.permission:<code>`).
## State of the Art
| Old Approach | Current Approach | When Changed | Impact |
|--------------|------------------|--------------|--------|
| `initialize` handshake + `Mcp-Session-Id` sessions | Stateless: per-request `_meta` (protocol version, client capabilities), `server/discover`, no session header | Spec 2026-07-28 (RC locked 2026-05-21, final 2026-07-28) [CITED: blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/] | D-25 pins pier to ≤ 2025-11-25 (Open Question 1) |
| GET SSE stream + `resources/subscribe` for change notifications | `subscriptions/listen` long-lived request (toolsListChanged, promptsListChanged…) and cacheable list results (`ttlMs`, `cacheScope`) | 2026-07-28 [VERIFIED: go-sdk@v1.8.0/docs/protocol.md:176-190] | Only relevant if the hybrid is adopted |
| SSE resumability (`Last-Event-ID`, EventStore) | Removed in 2026-07-28; kept for ≤2025-11-25 | 2026-07-28 | pier can skip EventStore; a dropped stream loses in-flight responses (client retries) |
| DCR as the main client registration | Client ID Metadata Documents preferred; DCR "for backwards compatibility" | Spec 2025-11-25 [CITED: modelcontextprotocol.io/specification/2025-11-25/basic/authorization] | wristband supports DCR only; acceptable, CIMD is a later improvement |
**Deprecated/outdated:**
- `StreamableHTTPOptions.CrossOriginProtection` is deprecated; wrap the handler with `http.NewCrossOriginProtection()` instead [VERIFIED: go-sdk@v1.8.0/mcp/streamable.go:184-195].
- Roots, Sampling and Logging are deprecated in 2026-07-28 (still work for a year); pier uses none.
## Assumptions Log
| # | Claim | Section | Risk if Wrong |
|---|-------|---------|---------------|
| A1 | The go-sdk and jsonschema-go module names are legitimate (Go ecosystem not checkable by the legitimacy seam) | Standard Stack, Package Legitimacy Audit | Supply-chain risk; mitigated by the human-verify checkpoint and the proxy Origin URLs |
| A2 | Mainstream MCP clients (Claude, ChatGPT, IDE agents) still fall back to 2025-11-25 `initialize` when a server rejects 2026-07-28 | Pitfall 2, Open Question 1 | A client speaking only 2026-07-28 cannot connect to a stateful pier |
| A3 | Binding the session to `(client_id, user_id)` (not to the token hash) matches the intent of D-25's "a different token … is refused" | Pattern 8 | If the user means the literal token, sessions break every hour on refresh |
| A4 | The admin set/remove path for links can live on the sm-user-plugin (frontend) Users screen and/or a CLI command, because cabana has no backend Users screen | Open Question 3 | If the user wants a new cabana backend-users screen, scope grows a lot |
| A5 | Group-only frontend permission semantics should stay as is for routes (pier mirrors them) | Pitfall 3, Open Question 4 | If the user expects tri-state user overrides, list filtering differs from the design note |
| A6 | `ai` scope plays no role in the read/write ceiling (D-20) | Open Question 6 | Tools might need `ai` gating |
| A7 | A memory session store is acceptable for single-instance apps (sm-demo-app has no Redis: compose.yaml runs only Postgres) | Pattern 8, Environment | If Redis is mandatory, the demo deployment needs Redis on rome |
| A8 | Granting `backend` only when a link exists at consent time (and dropping it otherwise) is acceptable UX | Security Domain | A later link would need re-consent (step-up) |
| A9 | Per-call binary growth of ~2.9 MiB is acceptable if pier is always linked | Open Question 2 | Every app grows even with pier disabled |
## Open Questions
1. **Stateful-only (D-25) vs hybrid with the 2026-07-28 stateless protocol.**
- What we know: the current spec removed sessions; the SDK serves 2026-07-28 only statelessly; SDK clients fall back to 2025-11-25 against a stateful server (probe).
- What's unclear: whether the user wants pier to also answer 2026-07-28 clients (stateless dispatch per request, `subscriptions/listen` for push), which is additive to D-25's stateful path.
- Recommendation: ship D-25 as locked; design the identity/dispatch/filter layers so a stateless handler can reuse them; raise it at plan confirmation. Do not silently adopt the hybrid.
2. **Always link pier, or behind a build tag?**
- What we know: linking the SDK costs ~2.9 MiB stripped; the framework already has the opt-in build-tag pattern (`summercms_s3`: `s3_on.go //go:build summercms_s3`, `s3_off.go //go:build !summercms_s3`, `summer.yaml build.tags`) [VERIFIED: modules/lagoon/attach/s3_on.go:1, s3_off.go:1, bucket.go:27-28].
- What's unclear: SC-1 says "enables the module in config"; a tag adds a build step.
- Recommendation: ask the user; if always linked, keep all plugin-facing types SDK-free anyway (they must be, for the tag option to remain open).
3. **D-04 "backend Users screen" does not exist.** cabana mounts no backend-users controller; admins come from `admin:create`/`admin:reset-password` commands [VERIFIED: modules/cabana/http.go:247-330, commands.go:21,45]. 19.6's Reset 2FA uses the user plugin's **own** Users screen (`AdminRecordActions` + a published `TwoFactorAdmin` slot), not a generic cabana seam (19.6-01-PLAN :49, :270), so there is nothing to reuse beyond that pattern. Recommendation: My Profile gets the generic cabana seam (D-04 part 1); admin set/remove goes on the sm-user-plugin **frontend** Users screen as a record action "Remove backend link" (Danger, `Applies` only when linked, permission) plus a way to set it (a form field with `DropdownOptions` of backend admins, or an `AdminAction` widget), and/or a `user:link-backend` console command. Needs user confirmation.
4. **Frontend permission semantics** (Pitfall 3): mirror the route (group grants) — recommended — or change `user.permission` to `MergedPermissions` (behaviour change in a core plugin).
5. **Session binding granularity** (A3): `(client_id, user_id)` vs token hash.
6. **`ai` scope**: ignore for tools (recommended) or require it for some tool class.
7. **MCP consent surface.** wristband sends the browser to `{issuer}/connect?request=` [VERIFIED: modules/wristband/authorize.go:174 — `spa := s.opts.Issuer + "/connect?request=" + rfc3986Escape(requestID)`]. Recommendation: sm-oauthserver-plugin adds JSON consent endpoints for the MCP server (show/decide, `jwt.auth`, mirroring fonoteka's `ConsentShow`/`ConsentStore` contract) that include the linked admin's display name when `backend` is requested; vue-demo-app adds `/connect` and the link-proof page. The demo's nginx does not route `/oauth` or `/.well-known` to Go yet (sm-demo-app/DEPLOY.md:34), so the deploy plan must add `/mcp`, `/.well-known/oauth-protected-resource*`, `/.well-known/oauth-authorization-server` and `/oauth/mcp/*`.
8. **Permission code spelling.** Existing backend codes are `golem15.users.*` (`golem15.users.access_users`, `…access_groups`, `…access_settings`, `…impersonate_user`, `…manage_privileged_groups` [VERIFIED: fonoteka.go/plugins/golem15/user/admin_permissions.go:17-41]); 19.6 adds `golem15.users.reset_two_factor`. Recommend `golem15.users.manage_backend_links` (CONTEXT's example `golem15.user.…` would break the pattern).
## Environment Availability
| Dependency | Required By | Available | Version | Fallback |
|------------|------------|-----------|---------|----------|
| Go toolchain | everything | ✓ | go1.27.2 | — |
| Docker | testcontainers Postgres suites | ✓ | 29.9.1 | `-short` skips; `sudo -u golem` if socket denied (CLAUDE.md) |
| redis-server | manual multi-instance smoke | ✓ | 7.2.5 | miniredis in unit tests |
| psql | manual checks | ✓ | 18.6 | — |
| Node / npm | admin SPA rebuild, vue-demo-app pages | ✓ | v22.23.3 / 12.2.1 | — |
| govulncheck | dependency audit in the decision note | ✗ | — | `go run golang.org/x/vuln/cmd/govulncheck@latest ./...` |
| Redis on the demo host (rome) | D-25 multi-instance | unknown (sm-demo-app compose has Postgres only; env.example has no `SUMMER_REDIS__URL`) | — | memory session store for the single-instance demo (A7) |
**Missing dependencies with no fallback:** none.
**Missing dependencies with fallback:** govulncheck (go run); Redis on rome (memory store).
## Validation Architecture
### Test Framework
| Property | Value |
|----------|-------|
| Framework | Go `testing` (go1.27.2) + testcontainers-go v0.44.0 (Postgres) + miniredis v2.40.0 + go-sdk v1.8.0 client; vitest 3.2.7 for the admin SPA seam |
| Config file | none (Go); `admin/vitest.config.*` exists for the SPA |
| Quick run command | `go test -short ./modules/pier ./modules/surf ./modules/bouncer ./modules/cabana ./modules/wristband ./modules/pact` |
| Full suite command | `go vet ./... && go test ./...` (framework) + `go test ./...` in each touched plugin (through the app workspace for sm-user-plugin) + `npm --prefix admin run test` + `scripts/check-phase19.7.sh` |
### Phase Requirements → Test Map
| Req | Behavior | Test Type | Automated Command | File Exists? |
|-----|----------|-----------|-------------------|-------------|
| SC-1/D-30 | disabled → no `/mcp`, no PRM routes in `route:list`; enabled → mounted | unit | `go test -short ./modules/surf -run TestPierMount` | ❌ Wave 0 |
| SC-1 | SSE stream flushes through the streaming route kind | unit (httptest, real flush) | `go test -short ./modules/surf -run TestStreamingRoute` | ❌ |
| SC-2/D-10..D-12 | bad binding/foreign route/duplicate name/invalid name fail boot; prefixing | unit | `go test -short ./modules/pier -run TestCatalog` | ❌ |
| D-11/D-13/D-14 | path/query/body split, raw body forwarding, isError with route body, annotations | unit (fake routes) | `go test -short ./modules/pier -run TestDispatch` | ❌ |
| D-15/D-16 | public/cookie-only route refused unless guard declared; requirement union | unit | `go test -short ./modules/pier -run TestGuardRules` | ❌ |
| D-17 | marker honoured by bouncer `api_token`/`backend` and `ResolveBackend`; header cannot forge it; frontend marker refused by backend guard | unit | `go test -short ./modules/bouncer ./modules/cabana -run TestDispatchMarker` | ❌ |
| SC-3/D-20/D-23 | per-principal tools/list via SDK client; read-only grant hides write tools; call refused when not listed | integration (miniredis, fake guards) | `go test -short ./modules/pier -run TestToolsList` | ❌ |
| D-25 | binding mismatch 403; DELETE ends session; rehydration on second router instance; sliding TTL expiry → 404 | integration (two routers, one miniredis) | `go test -short ./modules/pier -run TestSessions` | ❌ |
| D-26/D-27 | event → PUBLISH → list_changed received by SDK client on the other instance; revocation closes session | integration | `go test -short ./modules/pier -run TestFanOut` | ❌ |
| D-09 | plugin codes appear in user and group permission editors next to seeded rows; seeded rows win on duplicate code | Postgres | `go test ./... -run TestFrontendPermissionCatalog` (user plugin) | ❌ |
| D-03..D-08 | link request one-shot/expiry/fresh-iat/1:1 uniqueness/admin set records linked_by/event; per-request lookup | Postgres | `go test ./... -run TestBackendLink` (user plugin) | ❌ |
| D-04 | account seam: contributions listed, action runs, `interactiveOnly`, existing `/account*` and `/auth/me` payloads unchanged | unit + vitest | `go test -short ./modules/cabana -run TestAccountContributions`; `npm --prefix admin run test` | ❌ |
| D-21 | `backend` accepted at authorize, unchanged default; consent drops `backend` without a link; consent payload has the admin line only with a link | unit + Postgres | `go test -short ./modules/wristband -run TestMCPScopes`; plugin `go test ./... -run TestMCPConsent` | ❌ |
| SC-4 | `oas_at_` verifier: OIDC token refused, revoked/expired/foreign-kind refused | Postgres | plugin `go test ./... -run TestMCPTokenVerifier` | ❌ |
| SC-4/D-18 | end-to-end in sm-demo-app: OAuth token → user tool works; with link + backend scope → Journal create_post as the admin; unlink → tool gone and call refused | e2e (testcontainers + SDK client) | `cd ../sm-demo-app && go test ./... -run TestMCPEndToEnd` | ❌ |
| D-31 | fonoteka.go and sm-bm-app build and pass at bumped pins | build/test | `scripts/check-phase19.7.sh` stage `consumers` | ❌ |
### Sampling Rate
- **Per task commit:** the quick run command for the touched module(s).
- **Per wave merge:** framework `go vet ./... && go test ./...` plus the touched plugin suites.
- **Phase gate:** `scripts/check-phase19.7.sh` (fail-closed, stages like check-phase19.4.sh: go, plugins, userplugin additive check, consumers, docker, admin dist/openapi) green before `/msd-verify-work`.
### Wave 0 Gaps
- [ ] `scripts/check-phase19.7.sh` — phase gate (pattern: `scripts/check-phase19.4.sh`)
- [ ] pier test helpers: fake guard provider, fake verifier, a two-instance harness on one miniredis, an SDK client with a bearer-injecting `http.Client`
- [ ] user-plugin Postgres fixtures for links and frontend permission rows (extend `admin_harness_test.go`)
- [ ] additive-API checks for sm-user-plugin, sm-journal-plugin and sm-oauthserver-plugin (no exported identifier removed since the last pre-phase commit, as check-phase19.4.sh does)
- Frameworks: none to install (go-sdk is added by the decision-note plan after the human check).
## Security Domain
### Applicable ASVS Categories
| ASVS Category | Applies | Standard Control |
|---------------|---------|-----------------|
| V2 Authentication | yes | Every request re-verifies the bearer (D-25); `oas_at_` verifier checks prefix, hash, expiry, revocation, client kind `mcp`, user active; personal tokens only behind the flag (D-19) |
| V3 Session Management | yes | `crypto/rand.Text` session IDs; binding key check (403); sliding TTL; DELETE ends everywhere; revocation closes sessions; periodic re-verify on long GET streams |
| V4 Access Control | yes | Scope ceiling (read → readOnly tools only, `backend` scope required for backend guard), guard provider checks equal to route enforcement, route middleware still enforces every call, marker only from pier |
| V5 Input Validation | yes | JSON schema validation of arguments (`additionalProperties: false`), path value checks (no `/` in single-segment params), route `Where` constraints, body limits |
| V6 Cryptography | yes | No hand-rolled crypto; link codes and tokens stored as SHA-256 of 32 random bytes; constant-time compare where hashes are compared |
| V7 Error/Logging | yes | Audit log per tool call (tool, guard, frontend user id, backend admin id, client id, session id prefix, status, duration); never log tokens, arguments or bodies |
| V13 API | yes | Origin validation / DNS-rebinding protection (`http.NewCrossOriginProtection` or an allow-list; the SDK's localhost protection applies only to loopback listeners), PRM and `WWW-Authenticate`, `throttle:` bucket on `/mcp` |
### Known Threat Patterns for this stack
| Pattern | STRIDE | Standard Mitigation |
|---------|--------|---------------------|
| Forged dispatch marker (an HTTP request claiming a pre-resolved principal) | Spoofing | Context-only marker with unexported key in bouncer; nothing reads headers for it; test that a request with any header set is unaffected; grep gate that only pier calls the setter |
| Frontend marker accepted by a backend guard | Elevation | Marker lists the exact bouncer guard names to honour; `ResolveBackend` honours only a backend-kind marker |
| Token passthrough / confused deputy | Spoofing/Elevation | Inner request carries no `Authorization`/`Cookie`; spec "MUST NOT pass through the token" |
| Audience confusion (OIDC userinfo token or another resource's token used at `/mcp`) | Spoofing | Verifier accepts only `oas_at_` tokens of `kind = mcp` clients; single resource per issuer, `resource` equal to `mcp.resource`; consider persisting `resource` on MCP token rows (additive column) |
| Session hijack (stolen `Mcp-Session-Id` with another valid token) | Spoofing | Binding key check; IDs unguessable; spec "Binding session IDs to user information" (SDK `TokenInfo.UserID` semantics) |
| Link without proof / replayed link code / CSRF on confirm | Elevation | One-shot hashed code bound to the admin, short TTL, row lock, `jwt.auth` confirm with fresh `iat`, POST only, page names the admin; never by email (D-05) |
| Admin sets a link to any frontend user (D-07) | Elevation | Separate permission without a default role, `linked_by` recorded, event fired; documented as a privileged action in the security review |
| Consent phishing: DCR client asks for `backend` | Elevation | Consent shows the client name, redirect host and "also act as admin <name>"; grant `backend` only if a link exists at consent; `backend` revocable per grant |
| Superuser reach through an agent (D-22) | Elevation | Read/write ceiling; audit log; security review weighs an optional config ceiling that refuses superuser backend principals over MCP |
| Personal token at the MCP endpoint (spec says "MUST only accept tokens … valid for use with their own resources") | Spoofing | Off by default (D-19); tokens are this app's own; each capped by its own scope |
| Prompt injection via tool metadata | Tampering | Static, factual descriptions; no data-derived text in descriptions or prompt templates |
| DoS: many sessions/streams | DoS | `throttle:` on `/mcp`, session cap per binding key, idle TTL, SDK `MaxRequestBodyBytes` (4 MiB default), per-call timeout, cap on captured route response size |
## Sources
### Primary (HIGH confidence)
- go-sdk v1.8.0 source in the module cache (proxy-verified): `mcp/streamable.go`, `mcp/streamable_server.go`, `mcp/server.go`, `mcp/session.go`, `mcp/shared.go`, `mcp/tool.go`, `mcp/protocol.go`, `auth/auth.go`, `docs/protocol.md`, `README.md`, `LICENSE`, `go.mod`, `examples/server/distributed/main.go`
- jsonschema-go v0.4.3 source: `jsonschema/infer.go`, `LICENSE`, `go.mod`
- Local probes (scratchpad): linked-module list, +2.9 MiB size, stateful negotiation 2025-11-25, cross-instance rehydration, per-session list_changed
- Framework: `modules/surf/router.go`, `routetable.go`, `apidocs.go`, `serve.go`, `params.go`, `overlap.go`, `limiter.go`; `modules/bouncer/context.go`, `registry.go`, `guard.go`; `modules/cabana/contracts.go`, `token_guard.go`, `auth.go`, `http.go`, `account.go`; `modules/wristband/server.go`, `authorize.go`, `stores.go`; `modules/cooler/cooler.go`; `modules/pact/capabilities.go`; `modules/party/registry.go`; `internal/build/*`; `modules/lagoon/attach/s3_*.go`
- sm-user-plugin (fonoteka.go checkout, commit 35cf166 = v0.1.5): `plugin.go`, `routes.go`, `middleware/api_token.go`, `classes/permissions.go`, `classes/user_lookup.go`, `classes/api_tokens.go`, `classes/codes.go`, `models/*`, `controllers/*admin_controller.go`, `admin_permissions.go`, `config/config.yaml`
- sm-journal-plugin: `routes.go`, `plugin.go`, `controllers/api/auth.go`, `posts.go`, `posts_helpers.go`
- sm-oauthserver-plugin: `servers.go`, `routes.go`, `middleware.go`, `classes/mcp_store.go`, `models/access_token.go`, `README.md`
- 19.6 plans (files_modified, D-15 slot), 19.4 CONTEXT, design note and both folded todos
### Secondary (MEDIUM confidence)
- MCP authorization spec 2025-11-25 — https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization (PRM MUST, resource MUST, no passthrough)
- MCP 2026-07-28 release candidate post — https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/ (sessions and initialize removed)
### Tertiary (LOW confidence)
- Web search summaries on stateless MCP adoption ([appwrite.io](https://appwrite.io/blog/post/mcp-goes-stateless-in-the-2026-07-28-specification), [nango.dev](https://nango.dev/blog/stateless-mcp-how-it-changes-the-way-agents-call-tools), [betterstack](https://betterstack.com/community/guides/ai/mcp-stateless/)) — client behaviour claims not verified (A2)
## Metadata
**Confidence breakdown:**
- Standard stack: HIGH — versions from the Go proxy, source read, dependency graph and size measured locally; legitimacy unverifiable by the seam (Go), gated by a human check.
- Architecture: HIGH for in-repo seams (all read this session) and for the SDK session/rehydration/notification mechanics (probe-verified); MEDIUM for the overall hybrid-protocol question.
- Pitfalls: HIGH — each tied to a read line range or a probe.
**Research date:** 2026-10-21
**Valid until:** 2026-11-20 (go-sdk moves fast; re-check `@latest` before `go get`)

View File

@@ -0,0 +1,93 @@
---
phase: "19.7"
slug: "generic-mcp-module"
# status lifecycle: draft (seeded by plan-phase) → validated (set by validate-phase §6)
# audit-milestone §5.5 distinguishes NOT-VALIDATED (draft) from PARTIAL (validated + nyquist_compliant: false) (#2117)
status: draft
nyquist_compliant: false
wave_0_complete: false
created: "2026-10-21"
---
# Phase 19.7 — Validation Strategy
> Per-phase validation contract for feedback sampling during execution. Source: `19.7-RESEARCH.md` § Validation Architecture.
---
## Test Infrastructure
| Property | Value |
|----------|-------|
| **Framework** | Go `testing` + testcontainers-go (Postgres) + miniredis + go-sdk client; vitest for the admin SPA seam |
| **Config file** | none (Go); `admin/vitest.config.*` for the SPA |
| **Quick run command** | `go test -short ./modules/pier ./modules/surf ./modules/bouncer ./modules/cabana ./modules/wristband ./modules/pact` |
| **Full suite command** | `go vet ./... && go test ./...` (framework) + `go test ./...` in each touched plugin + `npm --prefix admin run test` + `scripts/check-phase19.7.sh` |
| **Estimated runtime** | ~120 seconds (quick ~20 s) |
---
## Sampling Rate
- **After every task commit:** quick run command for the touched module(s)
- **After every plan wave:** framework `go vet ./... && go test ./...` plus the touched plugin suites
- **Before `/msd-verify-work`:** `scripts/check-phase19.7.sh` green
- **Max feedback latency:** 120 seconds
---
## Per-Task Verification Map
Filled in by the planner per task; the behaviour → test map is:
| Ref | Behavior | Test Type | Automated Command | File Exists | Status |
|-----|----------|-----------|-------------------|-------------|--------|
| SC-1/D-30 | disabled → nothing mounted; enabled → `/mcp` mounted | unit | `go test -short ./modules/surf -run TestPierMount` | ❌ W0 | ⬜ pending |
| SC-1 | SSE stream flushes through the streaming route kind | unit | `go test -short ./modules/surf -run TestStreamingRoute` | ❌ W0 | ⬜ pending |
| SC-2/D-10..D-12 | binding/duplicate/name boot errors; prefixing | unit | `go test -short ./modules/pier -run TestCatalog` | ❌ W0 | ⬜ pending |
| D-11/D-13/D-14 | arg split, isError with route body, annotations | unit | `go test -short ./modules/pier -run TestDispatch` | ❌ W0 | ⬜ pending |
| D-15/D-16 | non-token routes refused; requirement union | unit | `go test -short ./modules/pier -run TestGuardRules` | ❌ W0 | ⬜ pending |
| D-17 | marker honoured in-process only; not forgeable from HTTP; frontend marker refused by backend guard | unit | `go test -short ./modules/bouncer ./modules/cabana -run TestDispatchMarker` | ❌ W0 | ⬜ pending |
| SC-3/D-20/D-23 | per-principal tools/list; read grant hides write tools | integration | `go test -short ./modules/pier -run TestToolsList` | ❌ W0 | ⬜ pending |
| D-25 | session binding, DELETE, cross-instance rehydration, TTL | integration | `go test -short ./modules/pier -run TestSessions` | ❌ W0 | ⬜ pending |
| D-26/D-27 | pub/sub list_changed; revocation ends session | integration | `go test -short ./modules/pier -run TestFanOut` | ❌ W0 | ⬜ pending |
| D-09 | plugin frontend permission codes in editors | Postgres | user plugin `go test ./... -run TestFrontendPermissionCatalog` | ❌ W0 | ⬜ pending |
| D-03..D-08 | link proof, one-shot, 1:1, admin set, per-request lookup | Postgres | user plugin `go test ./... -run TestBackendLink` | ❌ W0 | ⬜ pending |
| D-04 | account contribution seam; existing payloads unchanged | unit + vitest | `go test -short ./modules/cabana -run TestAccountContributions`; `npm --prefix admin run test` | ❌ W0 | ⬜ pending |
| D-21 | `backend` scope and consent line | unit + Postgres | `go test -short ./modules/wristband -run TestMCPScopes`; plugin `-run TestMCPConsent` | ❌ W0 | ⬜ pending |
| SC-4 | `oas_at_` verifier refuses OIDC/revoked/expired/foreign tokens | Postgres | oauthserver plugin `go test ./... -run TestMCPTokenVerifier` | ❌ W0 | ⬜ pending |
| SC-4/D-18 | end-to-end in sm-demo-app incl. unlink removes tools | e2e | `cd ../sm-demo-app && go test ./... -run TestMCPEndToEnd` | ❌ W0 | ⬜ pending |
| D-31 | consumers build and pass at bumped pins | build | `scripts/check-phase19.7.sh` stage `consumers` | ❌ W0 | ⬜ pending |
*Status: ⬜ pending · ✅ green · ❌ red · ⚠️ flaky*
---
## Wave 0 Requirements
- [ ] `scripts/check-phase19.7.sh` — phase gate (pattern: `scripts/check-phase19.4.sh`)
- [ ] pier test helpers: fake guard provider, fake verifier, two-instance harness on one miniredis, SDK client with a bearer-injecting `http.Client`
- [ ] user-plugin Postgres fixtures for links and frontend permission rows
- [ ] additive-API checks for sm-user-plugin, sm-journal-plugin, sm-oauthserver-plugin
---
## Manual-Only Verifications
| Behavior | Requirement | Why Manual | Test Instructions |
|----------|-------------|------------|-------------------|
| Real MCP client (Claude) connects once via OAuth and sees both tool sets | SC-4 | external client and browser consent | Add the demo's `/mcp` URL to a client, sign in, link an admin, request `backend`, call a Journal tool |
| go-sdk legitimacy check before `go get` | D-24 | supply-chain human check | Confirm module path, version and maintainers in the decision note |
---
## Validation Sign-Off
- [ ] All tasks have `<automated>` verify or Wave 0 dependencies
- [ ] Sampling continuity: no 3 consecutive tasks without automated verify
- [ ] Wave 0 covers all MISSING references
- [ ] No watch-mode flags
- [ ] Feedback latency < 120s
- [ ] `nyquist_compliant: true` set in frontmatter
**Approval:** pending

View File

@@ -0,0 +1,353 @@
---
phase: 19.8-console-commands-overhaul
plan: 03
subsystem: console
tags: [bonfire, diary, surf, conga, lagoon, cli, release, plugins]
requires:
- phase: 19.8-01
provides: diary module (component loggers, daily files, config/log.yaml)
- phase: 19.8-02
provides: bonfire global flags, Artisan list and help, Examples, CheckDocs, hints, Reported
provides:
- serve, queue:work, schedule:run and summer dev startup summaries and shutdown reports
- http request log by route pattern on component http, on by default
- Artisan checklist for every framework command, D-19 hints, TestCommandsDocumented gate
- SummerCMS v0.4.0 tag, pushed, with its changelog post
- fifteen shared plugins swept (help, examples, named loggers, warn-only TestCommandsDocumented), pushed
- ten plugin patch releases and the five applications' pointer-bump and regenerate commits
- email-proof release (sm-user-plugin v0.1.5, sm-sociallogin-plugin v0.1.2) with the app pins
affects: [19.8-04, every application on SummerCMS, the shared plugins]
actuals:
tokens: 146594 # chars/4 over the realized diffs of all repositories (go.sum and dist excluded)
tasks: 8
commits: 37 # MEASURED in summercms.go: git rev-list --count plan_head_before..plan_head_after; most are other sessions' (19.5, 19.6, 19.6.1, 19.7 planning) — this plan's summercms.go commits are 6 code + 1 planning before this SUMMARY
plan_head_before: f0ee26f39f6fd104df79a4cb759c7359be51b497
plan_head_after: 053c643527ef9ed8e3fdcee904737efc30dc2ac3
tech-stack:
added: []
patterns:
- "Plugin component logger: package-level `var pluginLog = diary.For(\"<code>\")` in a logger.go per package"
- "Warn-only TestCommandsDocumented in each plugin repository (bonfire.CheckDocs, t.Logf)"
- "Data output (--json, tokens, keys) through out.Println so -q keeps it"
key-files:
created:
- modules/surf/summary.go
- cmd/summer/commands_documented_test.go
- "../sm-*-plugin/commands_documented_test.go and logger.go files (15 repositories)"
modified:
- modules/surf/serve.go
- modules/surf/accesslog.go
- modules/lagoon/migrations.go
- modules/conga/worker.go
- internal/dev/watch.go
- docs/setup/upgrading.md
- "../sm-*-app/main.go (regenerated), go.mod, go.sum and plugin gitlinks"
key-decisions:
- "Framework v0.4.0 tagged at 99b6df4, not at master HEAD: commits other sessions made during the run (c1f5ded todo, 19.6/19.6.1/19.7 planning) stay unpushed"
- "Ten plugins mounted by applications got patch tags (user v0.1.6, journal v0.2.2, sitemap/menu/faq/paymentgateway/oauthserver/golem/feedback v0.1.1, pgstripe v0.1.3): sm-demo-app's go_mod_test refuses pseudo-version pins"
- "chat and forum require sm-user-plugin v0.1.2, the first release with classes.DeletingEvent; v0.1.1 did not build without a workspace"
- "sm-user-plugin swept on top of v0.1.5 (D-20 complete, nothing deferred)"
patterns-established:
- "Pattern: a plugin release that requires a new framework minor gets a patch tag, and applications pin the tag in go.mod together with the gitlink"
requirements-completed: [SC-3, SC-4, SC-5, D-04, D-05, D-06, D-07, D-11, D-17, D-18, D-19, D-20, D-21, D-22, D-23, D-24, D-25, D-26, D-27, D-28, D-29]
coverage:
- id: D1
description: "serve startup summary, shutdown report and http request log by route pattern"
requirement: D-04
verification:
- kind: unit
ref: "modules/surf#TestServeSummarySmoke, TestShutdownReportSmoke, TestAccessLogSmoke"
status: pass
- kind: integration
ref: "modules/surf/serve_lifecycle_test.go#TestServeLifecycle (Docker)"
status: pass
human_judgment: true
rationale: "The styled terminal block and its readability are visual"
- id: D2
description: "Every framework command documented, Artisan checklist flags, hints and result lines"
requirement: D-17
verification:
- kind: unit
ref: "cmd/summer/commands_documented_test.go#TestCommandsDocumented"
status: pass
- kind: other
ref: "go test -count=1 ./... with Docker (summercms.go)"
status: pass
human_judgment: false
- id: D3
description: "SummerCMS v0.4.0 tagged and pushed with the changelog post"
requirement: D-26
verification:
- kind: other
ref: "git ls-remote --exit-code --tags origin v0.4.0; https://summercms.io/changelog/summercms-0-4-0"
status: pass
human_judgment: true
rationale: "The changelog notes' wording is editorial"
- id: D4
description: "Fifteen shared plugins swept in one pushed commit each, pinned to v0.4.0"
requirement: D-20
verification:
- kind: unit
ref: "each plugin: go vet, go test -short, TestCommandsDocumented (no warnings), GOWORK=off build, build against the v0.4.0 tag"
status: pass
human_judgment: false
- id: D5
description: "Applications bumped and main.go regenerated; each binary boots list and help migrate"
requirement: D-26
verification:
- kind: other
ref: "go build ./... and ./bin/<app> list, ./bin/<app> help migrate in sm-demo-app, sm-bm-app, sm-grzybyfunkcjonalne-app, sm-summercmsio-app, fonoteka.go"
status: pass
human_judgment: false
- id: D6
description: "Landing copy and roadmap deck describe the new console (published, not deployed)"
requirement: SC-5
verification:
- kind: unit
ref: "vue-summercmsio-app pnpm test; sm-demo-app go test ./plugins/demo/roadmap/..."
status: pass
human_judgment: true
rationale: "Copy quality is editorial; the deploys are a separate user step"
duration: "segment B 3h 03m (2026-10-21T13:19:59Z to 16:23Z); segment A recorded in its own run"
completed: 2026-10-21
status: complete
---
# Phase 19.8 Plan 03: console sweep and release Summary
**serve/queue:work/schedule:run summaries and shutdown reports, the http request log by route pattern and an Artisan-documented command set shipped as SummerCMS v0.4.0 (tag, push, changelog), followed by fifteen swept shared plugins, ten plugin patch releases and pointer-bump plus regenerate commits in five applications; the 19.5 email-proof release went out first.**
## Performance
- **Segment A** (Tasks 1-5 and the Task 6 checkpoint): recorded in its own run.
- **Segment B** (email-proof release, Tasks 7 and 8): started 2026-10-21T13:19:59Z, completed 2026-10-21T16:23Z, about 3 h.
- **Tasks:** 8 of 8.
- **Repositories touched:** summercms.go, sm-summercmsio-app (and vue-summercmsio-app), sm-demo-app, sm-bm-app, sm-grzybyfunkcjonalne-app, fonoteka.go, sm-sociallogin-plugin and fifteen plugin repositories.
## Accomplishments
- `serve`: startup summary block (version, environment, URL, admin, database host:port/name, migration state, plugins, queue, scheduler, mail, cache, realtime, search), `startup summary` and `listening on <addr>` records off a terminal, shutdown report and exit 0. `queue:work`, `schedule:run` and `summer dev` (which now runs `serve`) do the same.
- Request log on by default under component `http` with the route pattern, never the path; `request_id` capped at 128 bytes.
- Application and tool commands: Long and Examples everywhere, `--json`, `--pretend` and `--force` where they fit, production confirmation on an interactive terminal only, result lines, D-19 hints, `TestCommandsDocumented` (fails) in cmd/summer.
- Released SummerCMS **v0.4.0** with the changelog post https://summercms.io/changelog/summercms-0-4-0.
- Fifteen shared plugins: imperative descriptions, Long, Examples, result lines, a `diary.For("<code>")` logger replacing every default-logger use, a warn-only `TestCommandsDocumented` (no warnings anywhere), README CLI sections and Dependencies naming v0.4.0. Added flags: `herald:vapid-generate --json`, `user:token:create --json`, `user:token:list --json`; progress bars on `journal:export-posts` and `user:backfill-handles`.
- Applications run the new console: sm-demo-app, sm-bm-app, sm-grzybyfunkcjonalne-app and sm-summercmsio-app pushed; fonoteka.go local.
- Landing copy and roadmap deck published with their repositories (no deploy).
## Task Commits
summercms.go (master):
1. **Task 1: serve summary, report and request log** - `c38dc1f` (feat)
2. **Task 2: worker and scheduler reports, summer dev runs serve** - `168d65c` (feat)
3. **Task 3: Artisan checklist for application commands, hints, coverage gate** - `9d06a78` (feat)
4. **Task 4: help, examples and hints for every tool command** - `3346060` (feat)
5. Test fixes after the Docker run (deviation 14): `2057643`, `4895b25` (fix)
6. Email-proof release record and closed todo: `99b6df4` (docs)
Task 5 (landing and deck): vue-summercmsio-app `473e476`, sm-summercmsio-app `44c96f2`, sm-demo-app `b325460`.
Task 6: checkpoint, reply "release v0.4.0".
Task 7 and Task 8: one commit per plugin and two per application, listed in the Handoff (sections 8 and 9).
## Handoff
### 1. Segment A commits
summercms.go (master, pushed in segment B):
| Task | Hash | Subject |
|------|------|---------|
| 1 | `c38dc1f` | feat(surf): serve startup summary, shutdown report and http request log |
| 2 | `168d65c` | feat(conga,dev): worker and scheduler reports, summer dev runs serve |
| 3 | `9d06a78` | feat: Artisan checklist for application commands, hints and coverage gate |
| 4 | `3346060` | feat(summer): help, examples and hints for every tool command |
summercms.go `origin/master..master` at the checkpoint: 43 commits, `c996790` (docs(roadmap): renumber console commands overhaul from phase 32 to 19.8) through `3346060`. They are this phase's commits (plans 01, 02, 03 and the phase planning docs), Phase 19.5's commits (plans 02-05 docs, tests, gate, security review, verification, UAT), `671d388` test(cooler) and `cb38c49` docs(notes). Latest tag on origin then: `v0.3.1`.
Sibling repositories (segment A, local until segment B):
| Repository | Branch | Hash | Subject |
|------------|--------|------|---------|
| sm-summercmsio-app/vue-summercmsio-app | master | `473e476` | feat: describe the Artisan-style console |
| sm-summercmsio-app | master | `44c96f2` | chore: bump vue-summercmsio-app for console copy |
| sm-demo-app | master | `b325460` | feat(demo): roadmap deck describes the new console |
sm-summercmsio-app also held `8581576` fix(scripts): changelog-post sends its own User-Agent (not this plan's); approved with the reply and pushed.
### 2. Framework API for the plugin sweep (names as built)
- `bonfire.Command` fields `Long string`, `Examples []bonfire.Example`, `Hidden bool`; `bonfire.Example{Command, Description string}`.
- `bonfire.CheckDocs(commands []bonfire.Command) []bonfire.DocProblem`; `DocProblem{Command string; Missing []string}` with `String()`.
- Errors: `bonfire.Reported(err) error`, `bonfire.WithHint(err, explanation, try string) error`, `bonfire.Usage(err) error`.
- Output helpers: `bonfire.IsQuiet`, `bonfire.Interactive`, `bonfire.IsTerminal`, `bonfire.ConfirmProceed`, `bonfire.Process`, `bonfire.Current`, `bonfire.Failed`, `bonfire.Details`, `bonfire.GlobalsOf`, `bonfire.VerbosityOf`. Data goes through `out.Println`/`out.Printf` (kept under `-q`).
- Logging: `diary.For("<component>") *slog.Logger`, `diary.HideOnTTY(ctx)`.
### 3. Release recommendation
- Recommended tag: v0.4.0; upgrading section `## Upgrading to 0.4` in `docs/setup/upgrading.md` (kept, the reply chose v0.4.0).
### 4. Repository state at the checkpoint (after `git fetch origin`)
| Repository | Branch | HEAD | Ahead/behind | Uncommitted | Framework require |
|------------|--------|------|--------------|-------------|-------------------|
| ../sm-paymentgateway-plugin | main | `16484ba` | 0/0 | none | v0.1.24 |
| ../sm-pgstripe-plugin | main | `c48c3b4` | 0/0 | none | v0.2.2 |
| ../sm-oauthserver-plugin | master | `0deb9a4` | 0/0 | none | v0.3.0 |
| ../sm-journal-plugin | main | `48d6593` | 0/0 | `?? go.work.sum` (not this plan's, left alone) | v0.2.0 |
| ../sm-sitemap-plugin | main | `46898c2` | 0/0 | none | v0.2.0 |
| ../sm-faq-plugin | main | `2efe351` | 0/0 | none | v0.2.0 |
| ../sm-menu-plugin | main | `f7eddd5` | 0/0 | none | v0.2.0 |
| ../sm-chat-plugin | main | `f4aeb87` | 2/0 (`0aee785`, `f4aeb87`, approved) | none | v0.2.2 |
| ../sm-chatchannels-plugin | main | `3738f45` | 2/0 (`7d49c3c`, `3738f45`, approved) | none | v0.2.2 |
| ../sm-chatvideo-plugin | main | `66b3bc7` | 0/0 | none | v0.2.3 |
| ../sm-forum-plugin | main | `6813f00` | 0/0 | none | v0.2.3 |
| ../sm-herald-plugin | main | `0c7de4d` | 0/0 | none | v0.2.3 |
| ../fonoteka.go/plugins/golem15/golem | master | `544aa28` | 0/0 | none | v0.1.9 |
| ../fonoteka.go/plugins/golem15/feedback | master | `e06b140` | 0/0 | none | v0.1.9 |
| ../fonoteka.go/plugins/golem15/user | master | `c7635c5` | 0/0 | none | v0.1.9 |
Applications: sm-demo-app `b325460` (1 ahead), sm-bm-app `99af8c2` (another session's `.planning/*` and tool directories uncommitted, left alone), sm-grzybyfunkcjonalne-app `6fc1a34` (1 ahead, approved), sm-summercmsio-app `44c96f2` (2 ahead), fonoteka.go `f4d5728` (31 ahead, never pushed).
### 5. Docker tests
- `TestServeLifecycle` (modules/surf): **PASS**. It was NOT RUN in segment A (no traffic reached containers on this host); after the host networking was fixed it failed on a missing app key and body limits in its fixture (deviation 14, `4895b25`) and then passed.
- Full `go test -count=1 ./...` with Docker in summercms.go after `4895b25`: **PASS** (`TestQueueWork`, `TestScheduleRunForeground` fixed in `2057643`).
- Plugin Docker suites in segment B: PASS in paymentgateway, pgstripe, oauthserver, journal, chat, chatchannels, forum, chatvideo, herald, golem, feedback; pre-existing failures in sitemap, faq, menu and the user plugin's `updates` package (section 8, deferred-items.md).
### 6. Phase 19.5 and the user plugin
- Phase 19.5 completed and closed during segment A's pause; its commits (`7170c5b` … `9ab28e1`) went out with the framework push.
- The email-proof release ran first in segment B (section 8). sm-user-plugin was then swept on top of v0.1.5 (`9986342`, tag v0.1.6): **D-20 complete**, nothing deferred, no `user-plugin-console-sweep.md` todo.
### 7. Deviations so far
See `## Deviations from Plan` below.
### 8. Segment B: email-proof release and Task 7 results (recorded before Task 8)
- Segment B start check: every segment A hash resolves; repository states matched the handoff except the email-proof release made first and Phase 19.5's documented commits in summercms.go (`7170c5b` … `9ab28e1`).
- Email-proof release (todo `email-proof-release.md`, steps 0-5): sm-user-plugin `v0.1.5` at `35cf166` (pushed `c7635c5..35cf166`); sm-sociallogin-plugin `9fd58db` and `v0.1.2` (pushed `d7ee28d..9fd58db`); sm-demo-app `f16ef6c`, `1afd7ff`, `a5703f9` (snapshot sync) pushed `61a00e9..a5703f9` with `b325460`; sm-bm-app `1e09c23`, `cc4b23f` pushed `99af8c2..cc4b23f` with `5fd0cc3`; fonoteka.go `2be9e1f` local. Recorded in 19.5-05-SUMMARY.md and the closed todo (summercms.go `99b6df4`).
- Framework: master pushed `5cf5c4f..99b6df4` (64 commits); annotated tag **v0.4.0** at `99b6df4` (tag object `e30fd34`), pushed. `c1f5ded` (a todo capture another session committed during this run) and later 19.6, 19.6.1 and 19.7 planning commits were left local, not pushed.
- Changelog: https://summercms.io/changelog/summercms-0-4-0 (post id 35, published at the tag date 2026-10-21T13:43:16Z).
- Plugins, one commit each `feat: console help, examples and named loggers (SummerCMS v0.4.0)`, pushed; CheckDocs warnings: none.
| Repository | Branch | Commit | Pushed range |
|------------|--------|--------|--------------|
| sm-paymentgateway-plugin | main | `e0badb5` | `16484ba..e0badb5` |
| sm-pgstripe-plugin | main | `6d07d98` | `c48c3b4..6d07d98` |
| sm-oauthserver-plugin | master | `d31f8e7` | `0deb9a4..d31f8e7` |
| sm-journal-plugin | main | `ffa2c8f` | `48d6593..ffa2c8f` |
| sm-sitemap-plugin | main | `96c790d` | `46898c2..96c790d` |
| sm-faq-plugin | main | `b3cc978` | `2efe351..b3cc978` |
| sm-menu-plugin | main | `8656474` | `f7eddd5..8656474` |
- Checks per plugin: `go vet`, `go test -short`, `GOWORK=off go build ./...`, the warn-only `TestCommandsDocumented` (PASS, no warnings), and a build against the published v0.4.0 tag from a go.mod copy without the framework replace. Full Docker suites pass in paymentgateway, pgstripe, oauthserver and journal; sitemap (`TestSitemapAdminSmoke`), faq (`TestFaqAdminSmoke`, `TestFaqAdminPurgesSiteCache`) and menu (`TestMenuAdminSmoke`, `TestMenuAdminPurgesSiteCache`) have admin smoke tests that already fail at the pushed HEAD before the sweep and with framework v0.3.1 (validation messages arrive translated where the tests expect phrase keys); logged in deferred-items.md.
### 9. Task 8 results
Plugins, one commit each, pushed (CheckDocs warnings: none):
| Repository | Branch | Commit | Pushed range | Notes |
|------------|--------|--------|--------------|-------|
| sm-chat-plugin | main | `5fea654` | `4d0e764..5fea654` (with approved `0aee785`, `f4aeb87`) | requires sm-user-plugin v0.1.2 |
| sm-chatchannels-plugin | main | `f3ac5d1` | `9026cd6..f3ac5d1` (with approved `7d49c3c`, `3738f45`) | tidy raises sm-user-plugin to v0.1.2 |
| sm-forum-plugin | main | `2779341` | `6813f00..2779341` | requires sm-user-plugin v0.1.2 |
| sm-chatvideo-plugin | main | `be688fe` | `66b3bc7..be688fe` | result line `Ended N video sessions.` |
| sm-herald-plugin | main | `74c889c` | `0c7de4d..74c889c` | `herald:vapid-generate --json` |
| sm-golem-plugin | master | `37748ae` | `544aa28..37748ae` | no default-logger sites; `feat: console help and examples` |
| sm-feedback-plugin | master | `ce4b43e` | `e06b140..ce4b43e` | |
| sm-user-plugin | master | `9986342` | `35cf166..9986342` | `--json` on token create/list, backfill progress, planning IDs removed |
Plugin tags (annotated, pushed): sm-user-plugin `v0.1.6` (`9986342`), sm-journal-plugin `v0.2.2` (`ffa2c8f`), sm-sitemap-plugin `v0.1.1` (`96c790d`), sm-menu-plugin `v0.1.1` (`8656474`), sm-faq-plugin `v0.1.1` (`b3cc978`), sm-paymentgateway-plugin `v0.1.1` (`e0badb5`), sm-pgstripe-plugin `v0.1.3` (`6d07d98`), sm-oauthserver-plugin `v0.1.1` (`d31f8e7`), sm-golem-plugin `v0.1.1` (`37748ae`), sm-feedback-plugin `v0.1.1` (`ce4b43e`). chat, chatchannels, forum, chatvideo and herald are not tagged (no application mounts them).
Applications (pointer bump, then regenerated main.go; `bin/<app> list` and `bin/<app> help migrate` exit 0; `grep -c 'bonfire.Execute(' main.go` is 1):
| Application | Bump commit | Regenerate commit | Pushed range |
|-------------|-------------|-------------------|--------------|
| sm-demo-app | `8d3a7f0` (user, journal, sitemap, menu, faq, paymentgateway, pgstripe, oauthserver, golem, feedback; go.mod pins the tags) | `7dc5022` | `a5703f9..7dc5022` |
| sm-bm-app | `d1992f1` (user v0.1.6, tidy for v0.4.0) | `87c7442` | `cc4b23f..87c7442` |
| sm-grzybyfunkcjonalne-app | `797ba0f` (user, journal, sitemap, menu, faq; tidy for v0.4.0) | `0bddc5b` | `6b9d13d..0bddc5b` (with approved `6fc1a34`) |
| sm-summercmsio-app | `6eb30f3` (user, journal; tidy for v0.4.0) | `289aa77` | `752099d..289aa77` (with approved `8581576` and Task 5's `44c96f2`); vue-summercmsio-app `8771773..473e476` pushed first |
| fonoteka.go | `59aa058` (user, golem, feedback; tidy) | `03de3e1` | not pushed (local only, 34 commits ahead) |
- g15office: no g15office tool was available in this session; the Phase 19.8 milestone task was neither found nor commented, and `.planning/G15OFFICE.md` is unchanged.
## Files Created/Modified
- Framework (segment A): `modules/surf/{serve.go,summary.go,accesslog.go,routelist_command.go}`, `modules/lagoon/{migrations.go,commands.go,keygen.go,connection.go,encrypted.go}`, `modules/conga/{worker.go,commands.go}`, `modules/cabana/*commands.go`, `modules/lighthouse/centrifugo/commands.go`, `modules/flare/commands.go`, `modules/bonfire/{errors.go,helpers.go}`, `modules/diary/*`, `internal/dev/watch.go`, `cmd/summer/*`, docs pages and READMEs listed in segment A.
- Each swept plugin: console command files, `commands_documented_test.go`, `logger.go` per package that logged, README, go.mod, go.sum.
- Each application: plugin gitlinks, go.mod, go.sum, main.go.
## Decisions Made
- v0.4.0 tagged at `99b6df4` (pushed explicitly) so commits other sessions made during the run stay local.
- Patch tags for the ten plugins applications mount: sm-demo-app refuses pseudo-version pins (`go_mod_test.go`).
- Package-level `pluginLog = diary.For("<code>")` in a `logger.go` per package; log messages unchanged (they keep their `golem15.<code>:` prefixes).
- The `subscriptions` package of sm-pgstripe-plugin is its own plugin and logs as component `pgstripesubscriptions`.
## Deviations from Plan
Segment A (1-14):
1. **[Rule 2 - API] bonfire.IsTerminal, bonfire.Details, bonfire.Detail.** Added as additive helpers for surf and conga. Commit `c38dc1f`.
2. **[Rule 2 - API] conga.WorkerStats.HandedBack.** Distinguishes jobs Stop handed back to the queue (River retries them) from failures. Commit `c38dc1f`.
3. **[Rule 3 - Blocking] lagoon.DatabaseLabel.** Exported DSN-to-"host:port/name" helper used by surf and conga. Commit `168d65c`.
4. **[Interpretation] PendingMigrations includes the framework sets** before the plugins, matching `Migrate`. Commit `c38dc1f`.
5. **[Interpretation] migrate:status --json** prints `{plugin, table, id, applied}` (gormigrate stores only the ID). Commit `9d06a78`.
6. **[Rule 1 - Bug] summer dev restart order.** Stops the old server before starting the new one. Commit `168d65c`.
7. **[Rule 2] summer dev gives the child the terminal.** Commit `168d65c`.
8. **[Rule 3 - Blocking] blog:publish coverage moved to its own package** (`docs/examples/blog/console/publish_test.go`). Commit `9d06a78`.
9. **[Interpretation] app.key hint in lagoon's encrypted.go, uploads-bucket hint in surf's publishUploads.** Commit `9d06a78`.
10. **[Interpretation] serve admin status folded into the summary's Admin row.** Commit `c38dc1f`.
11. **[Rule 3] sm-demo-app names.go** needed phase 19.8's Polish title. Commit `b325460`.
12. **[Interpretation] Landing tests** need `pnpm generate` before `pnpm test`.
13. **[Concurrency] sm-summercmsio-app held `8581576`** (not this plan's); reported at the checkpoint, approved with the reply, pushed.
14. **[Rule 1 - Bug] Docker-backed tests after host networking was fixed.** `TestQueueWork/runs_until_cancelled` and `TestScheduleRunForeground` asserted pre-19.8 Info lines; `TestServeLifecycle` lacked an app key and body limits. Commits `2057643`, `4895b25`; the full Docker suite then passed.
Segment B (15-27):
15. **[Concurrency] Framework push and tag at `99b6df4`, not master HEAD.** `c1f5ded` (another session's todo capture, committed during the run) was not in the approved range, so `99b6df4:refs/heads/master` was pushed and tagged; `c1f5ded` and the later 19.6, 19.6.1 and 19.7 planning commits stay local for their sessions.
16. **[Rule 3 - Blocking] sm-sociallogin-plugin is not readable over anonymous https.** `go get` of it (and the tag-proof builds) ran with a process-scoped `url.git@git.golem15.com:.insteadOf https://git.golem15.com/` (environment variables only; no git or Go configuration changed).
17. **[Rule 3 - Blocking] Plugin patch tags.** The plan pinned applications by submodule commit only; sm-demo-app's go.mod pins every plugin by version and `go_mod_test.go` refuses pseudo-versions, so the ten plugins applications mount were tagged (Handoff 9) and sm-demo-app's go.mod moved to those tags.
18. **[Rule 3 - Blocking] chat and forum require sm-user-plugin v0.1.2.** Both use `userclasses.DeletingEvent` (chat through the approved `0aee785`, forum already on its pushed HEAD) while requiring v0.1.1, which lacks it; `GOWORK=off go build` failed. Raised to v0.1.2 in their sweep commits; chatchannels and chatvideo follow through tidy.
19. **[Rule 3] Application commits include go.mod and go.sum.** The plan said to commit only the submodule paths; the version pins (sm-demo-app, sm-bm-app) and the framework v0.4.0 requirement with its new dependencies (`GOWORK=off go mod tidy`) belong in the same bump commit, or the applications do not build without a workspace.
20. **[Checklist changes]** `journal:import-posts` progress declined: it prints one line per post inside one transaction, which a bar would interleave (export got the bar). `journal:import-posts`, `sitemap:import` and `sitemap:import-winter` result lines move from Info to Success (text unchanged). `chatvideo:reconcile-sessions` prints `Ended N video sessions.` instead of `reconciled N session(s)`. `oauthserver:prune` gains `Pruned N expired rows and K signing keys.` after its six count lines. The pg sweeps keep their existing `Canceled N payments.`/`Canceled N orders.`/`Finished N orders.` and `Updated N of M currencies.` lines. `oauthserver:rotate-keys`, `golem:import-settings` and `feedback:import-settings` return `bonfire.Reported` after printing their own error, so it prints once.
21. **[Interpretation] sm-golem-plugin has no default-logger use,** so it got no logger file and its commit is `feat: console help and examples (SummerCMS v0.4.0)`.
22. **[Concurrency] fonoteka.go golem and feedback gitlinks.** Another session had left their checkouts at the v0.1.0 pin commits (`544aa28`, `e06b140`) unstaged; the bump to the sweep commits fast-forwards through them, so `59aa058` includes that move.
23. **[Environment] fonoteka's binary needs `SUMMER_GOLEM15__USER__JWT__SECRET` to boot** (`list` and `help migrate`); checked with a throwaway test value. Unchanged behaviour of the user plugin.
24. **[Concurrency] Roadmap deck not re-synced at the final push.** `sync-roadmap.sh --check` fails against local master only because of other sessions' unpushed planning; it passes against `origin/master`, so the deck was not synced to unpublished planning (deferred-items.md).
25. **[Email-proof] sm-demo-app snapshot sync `a5703f9`** before its push, as the todo prescribes; sm-sociallogin-plugin's tidy also recorded the framework's charm indirect requirements (its go.mod replaces the framework with the local checkout).
26. **[State] `state.advance-plan` and `state.record-session` not run.** STATE.md's current position belongs to the session executing 19.6/19.6.1 and holds its uncommitted edits; only the phase-scoped handlers ran (record-metric, add-decision, update-progress), and STATE.md is not committed here.
27. **[Tooling] g15office unavailable.** No comment on the Phase 19.8 milestone task; email-proof todo step 6 (comment on `onunbz3l`, Testing, `sqnwn1zi` to Live) also open.
**Total deviations:** 27 (segment A 14, segment B 13). **Impact:** no command name or flag was removed or renamed; the additions are flags, help, result lines and loggers. The tags and pushes stayed within the reply (framework, plugins, applications except fonoteka.go); nothing was deployed or force-pushed.
## Issues Encountered
- Pre-existing test failures in sitemap, faq, menu and the user plugin's `updates` package (deferred-items.md); each reproduced before the sweep and with framework v0.3.1.
- Application binaries print plugin boot INFO records before `list` output (tracked by another session's todo `c1f5ded`).
## User Setup Required
None.
## Next Phase Readiness
- Plan 19.8-04 (unit tests) can run against the released v0.4.0 and the swept plugins.
- Open for the user: deploy the applications (sm-demo-app, sm-bm-app, sm-grzybyfunkcjonalne-app, sm-summercmsio-app) when ready; the user plugin's two additive migrations (`users.handle`, `users.email_verified_at`) reach grzyby and summercms.io with this bump. Re-run `/msd-verify-work 19.5` (email-proof released). g15office updates for 19.5 and 19.8.
## Task 6 reply
> release v0.4.0
Chosen tag: **v0.4.0**. Recorded with the reply: full release (framework tag, push, changelog post; plugin sweeps pushed; application pointer-bump and regenerate commits pushed). The user also approved that unpushed commits made outside this plan go out with the release (sm-summercmsio-app `8581576`, sm-chat-plugin `0aee785` `f4aeb87`, sm-chatchannels-plugin `7d49c3c` `3738f45`, sm-grzybyfunkcjonalne-app `6fc1a34`, sm-demo-app `b325460` and similar earlier-session commits in the same repositories). fonoteka.go is never pushed (local commits only). The email-proof release (sm-user-plugin v0.1.5, sm-sociallogin-plugin v0.1.2) runs first, before Task 7.
---
*Phase: 19.8-console-commands-overhaul*
*Completed: 2026-10-21*
## Self-Check: PASSED
All commits listed resolve in their repositories (summercms.go 7, sociallogin 1, sm-demo-app 6, sm-bm-app 4, grzybyfunkcjonalne 2, summercmsio 3 plus vue 1, fonoteka.go 3, fifteen plugin sweep commits); the key files exist; the Handoff heading is present once; tags v0.4.0 and the eleven plugin tags resolve on their remotes.

View File

@@ -0,0 +1,301 @@
---
phase: 19.8-console-commands-overhaul
plan: 04
subsystem: console
tags: [tests, gate, diary, bonfire, slogtest, goldens, surf, conga, lagoon, cabana, flare, dev]
requires:
- phase: 19.8-01
provides: diary (channels, components, daily files, console handler)
- phase: 19.8-02
provides: bonfire global flags, list, help, errors, widgets, prompts
- phase: 19.8-03
provides: swept framework commands, request log, summaries, v0.4.0, swept plugins, applications
provides:
- "scripts/check-phase19.8.sh: fail-closed gate (self-test, go, hello, docs, plugins, apps, landing, docker) with --self-test"
- "Unit and golden tests for diary, sunscreen, bonfire, docsite and every swept framework command"
- "fix(bonfire): PeekGlobals reads clustered shorthands, released as SummerCMS v0.4.1 with its changelog post"
affects: [19.8 verification, the next framework tag]
actuals:
tokens: 53163 # chars/4 over the added lines of this plan's diff (goldens excluded)
tasks: 6 # Tasks 1-4, the Task 5 checkpoint and Task 6 (release v0.4.1)
commits: 11 # MEASURED: git rev-list --count plan_head_before..plan_head_after
plan_head_before: b906a31c368ad6a59ea8f6ac82121714ffad20da
plan_head_after: 72b8383c1022a993b66c6cd8b4f739bdee062246
tech-stack:
added: []
patterns:
- "Goldens under modules/bonfire/testdata with an -update flag; color detection cleared per test (plainEnv)"
- "testing/slogtest for the console handler through a key=value line parser, and for the full pipeline through JSON"
- "Interactive-terminal tests on a pseudo-terminal opened with syscall only (/dev/ptmx), no new dependency"
- "Gate additive check finds each plugin's sweep commit by subject and diffs it against its parent"
key-files:
created:
- scripts/check-phase19.8.sh
- modules/diary/diary_test.go
- modules/diary/config_test.go
- modules/diary/daily_test.go
- modules/diary/console_test.go
- modules/diary/slogtest_test.go
- modules/bonfire/globals_test.go
- modules/bonfire/root_test.go
- modules/bonfire/golden_test.go
- modules/bonfire/errors_test.go
- modules/bonfire/docs_test.go
- modules/bonfire/widgets_test.go
- modules/bonfire/live_test.go
- modules/bonfire/version_test.go
- modules/bonfire/testdata/*.golden (9 files)
- internal/docsite/check_commands_test.go
- modules/surf/summary_test.go
- modules/conga/stats_test.go
- modules/conga/logger_test.go
- modules/conga/confirm_linux_test.go
- modules/lagoon/pending_test.go
- modules/lagoon/commands_flags_test.go
- modules/lagoon/logger_test.go
- modules/lagoon/pty_linux_test.go
- modules/cabana/token_json_test.go
modified:
- modules/bonfire/globals.go
- modules/bonfire/version.go
- modules/sunscreen/sunscreen_test.go
- modules/surf/accesslog_test.go
- modules/flare/commands_test.go
- internal/dev/watch_test.go
- cmd/summer/runtime_test.go
key-decisions:
- "PeekGlobals reads a single-dash cluster letter by letter like pflag (-qn, -vq, -nvv) and stops at the first non-global letter; released as SummerCMS v0.4.1"
- "BuildVersion reads build info through an unexported package variable (refactor, no behaviour change) so tests pin the tagged, replaced and go.work cases"
- "Interactive confirmation is tested on a real pseudo-terminal (syscall ioctls on /dev/ptmx) instead of adding a pty dependency or a bonfire test seam"
- "The landing stage runs nuxt generate before the TAP suite and checks the roadmap snapshot against origin/master; TestTerminalCommands (deployed one-liner) is opt-in"
requirements-completed: [SC-5, D-03, D-08, D-09, D-10, D-12, D-13, D-14, D-15, D-16, D-17, D-18, D-19, D-21, D-23, D-25, D-27, D-28, D-29]
coverage:
- id: D1
description: "Fail-closed gate: detectors proven on planted output, additive and NOT SWEPT rules on planted git repositories, docker NOT RUN rule"
requirement: SC-5
verification:
- kind: other
ref: "bash scripts/check-phase19.8.sh --self-test"
status: pass
human_judgment: false
- id: D2
description: "diary config rules, floors, verbosity, quiet, daily rotation/retention/permissions/degradation, path confinement, redaction on every driver, control characters, lifecycle, concurrency, slogtest"
requirement: D-08
verification:
- kind: unit
ref: "modules/diary#TestConfigRules ... TestSlogtestPipeline (27 names in the gate)"
status: pass
human_judgment: false
- id: D3
description: "bonfire goldens (list, list --all, JSON, styled, help, passthrough help, three error blocks), flags, exit codes, duplicates, version header"
requirement: D-13
verification:
- kind: unit
ref: "modules/bonfire#TestListGolden, TestHelpGolden, TestErrorGolden, TestPeekGlobals, TestGlobalFlagsAgree, TestExitCodes"
status: pass
human_judgment: true
rationale: "The goldens were created with -update and read against the D-13, D-14 and D-18 previews; layout taste is a person's call"
- id: D4
description: "Widget matrix, live models, prompts, ConfirmProceed, Secret under -n, PrintAbove"
requirement: D-03
verification:
- kind: unit
ref: "modules/bonfire#TestWidgetMatrix, TestLiveModels, TestPromptsNoInteraction, TestSecretWithNoInteraction, TestConfirmProceed, TestPrintAbove"
status: pass
- kind: manual_procedural
ref: "throwaway program under script(1) on a pty: spinner, progress and process view print their final frames"
status: pass
human_judgment: false
- id: D5
description: "Swept framework commands: serve summary/records/report, request log, WorkerStats, PendingMigrations, --pretend, production question, --json, -q data, Reported, summer dev, delegates"
requirement: D-29
verification:
- kind: unit
ref: "go stage names for surf, conga, lagoon, flare, internal/dev, cmd/summer"
status: pass
- kind: integration
ref: "docker stage: TestServeLifecycle, TestPendingMigrations, TestMigratePretend, TestRollbackPretend, TestDeferredPurgePretend, TestMigrateStatusJSON, TestTokenCreateJSON, TestTokenListJSON, TestTokenCreateQuietKeepsToken"
status: pass
human_judgment: false
- id: D6
description: "Plugins (15 repositories), applications (5) and landing run from the gate"
requirement: D-17
verification:
- kind: other
ref: "bash scripts/check-phase19.8.sh --all (PHASE198_REQUIRE_DOCKER=1): every stage PASS"
status: pass
human_judgment: false
duration: 69min
completed: 2026-10-21
status: complete
---
# Phase 19.8 Plan 04: unit tests and the phase gate Summary
**A fail-closed `scripts/check-phase19.8.sh` (eight stages, green with `--all` and Docker required) plus unit, golden, slogtest and pseudo-terminal tests for diary, bonfire and every swept framework command; the tests found one bug in released code, `PeekGlobals` ignoring clustered shorthands such as `-qn`, fixed in its own commit and waiting on the Task 5 go-ahead.**
## Performance
- **Started:** 2026-10-21T16:47:08Z
- **Checkpoint reached:** 2026-10-21T17:56Z (about 69 min)
- **Tasks:** 4 of 6 done; Task 5 (checkpoint:decision) presented; Task 6 waits on its reply
- **Files:** 40 changed, 6,036 insertions, 7 deletions
## Accomplishments
- Gate with stages `self-test`, `go`, `hello`, `docs`, `plugins`, `apps`, `landing`, `docker`; detectors for `go test -json` and node `--test` TAP output, the additive check and the NOT SWEPT rule, each proven on planted input.
- diary: 24 new tests incl. `testing/slogtest` conformance for the console handler and the pipeline, 100 concurrent writers under `-race`, rotation with an injected clock, retention, 0640 files under a strict umask.
- bonfire: 9 goldens and 26 new tests (global flags, exit codes, widgets, live models, prompts, version header).
- Framework commands: summaries and reports, request log (never the path), WorkerStats, the production question on a real pseudo-terminal, `--pretend`, `--json`, `-q` data, Reported failures, `summer dev` with a real child process.
- `--all` runs the 15 swept plugins (short suites, coverage test, `GOWORK=off` build, additive check), the 5 applications (build, `summer build` leaves `main.go` unchanged, `list`, `help migrate`), the landing site and the Docker suites.
## Task Commits
1. **Task 1 (tracer): gate with the smoke tests by name**: `9b2fa6c` test(19.8)
2. **Task 2: diary, sunscreen, River and GORM**: `24f4643` test(diary), `db83a14` test(conga,lagoon)
3. **Task 3: bonfire and docsite**: `efb043e` fix(bonfire) (post-release fix), `9641a1b` refactor(bonfire), `7b58ad5` test(bonfire) goldens, `eeeee59` test(bonfire) flags/exit codes/widgets, `fd52868` test(docsite)
4. **Task 4: framework commands and the remaining stages**: `6720cae` test(surf,conga,lagoon), `2baec56` test(cabana,flare,dev,summer), `72b8383` test(19.8) gate stages
5. **Task 5**: checkpoint presented (see below)
6. **Task 6**: waits on the Task 5 reply
`commits: 11` is measured from the ledger (`b906a31..72b8383`); all eleven are this plan's. The tracer feedback gate re-ran Task 1's automated verify after its commit (green), so expansion continued.
## Post-release fixes
| # | Commit | Module | Symptom | Pinning test | In v0.4.0 | Needs require (plugins) | Regenerate (apps) |
|---|--------|--------|---------|--------------|-----------|-------------------------|-------------------|
| 1 | `efb043e` | bonfire | `PeekGlobals` read only `-v` clusters, so `-qn`, `-vq`, `-nvv` were missed before cobra parsed them: the generated main's pre-boot diary setup ignored them (boot logs not quieted), `Execute`'s error block rendered without the `-v` chain, and passthrough commands (summer's delegates) saw no globals. cobra itself parsed them, so the commands ran with the right options. | `TestPeekGlobals`, `TestGlobalFlagsAgree` (rows `combined -qn`, `combined -vq`, `combined -nvv`) | yes | no (no API change) | no (generated `main.go` unchanged; applications pick it up with a framework require bump) |
Not a fix, nothing to publish: `9641a1b` refactor(bonfire) reads build info through an unexported variable for `TestVersionHeader`; no behaviour change, it rides with the next tag.
Proposed patch tag: **v0.4.1** (framework only).
## Task 5 reply
> release v0.4.1, and yes, planning docs can go out
Before the reply `git tag --points-at HEAD` printed nothing (no tag on HEAD). Chosen tag: **v0.4.1** (framework only; no plugin require bumps, no application regenerate commits). The other sessions' unpushed planning-doc commits go out with the fix.
## --all stage summary
`PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all` at `72b8383`:
```
PASS self-test
PASS go
PASS hello
PASS docs
PASS plugins
PASS apps
NOT RUN TestTerminalCommands (the deployed one-liner end to end: needs network, Docker and ports 54320 and 8080; set SUMMERCMS_TERMINAL_CHECK=1)
PASS landing
PASS docker
Phase 19.8 gate passed
```
`go vet ./...`, `go test -count=1 -short ./...` and `--self-test` were green in the same run. No line of the log starts with `FAIL` or reports an unimplemented stage.
## Docker
- **Run.** The docker stage ran (daemon up, `PHASE198_REQUIRE_DOCKER=1`): `TestServeLifecycle`, `TestPendingMigrations`, `TestMigratePretend`, `TestRollbackPretend`, `TestDeferredPurgePretend`, `TestMigrateStatusJSON`, `TestTokenCreateJSON`, `TestTokenListJSON`, `TestTokenCreateQuietKeepsToken`: PASS.
- The complete Docker suites of `modules/lagoon/...`, `modules/cabana`, `modules/surf` and `modules/conga` (`go test -count=1 ./...` without `-short`) also pass with the new tests.
- Plugins: only their short suites run in the gate; the known Docker failures of sitemap, faq, menu and the user plugin's `updates` package (deferred-items.md, plan 03) are not part of this gate.
## Goldens against the previews
Read after `-update`; sections, order and alignment match D-13, D-14 and D-18. Intentional differences: the list also shows `help`; help prints the value placeholder as the upper-cased flag name (`--expires=EXPIRES`, the preview shows `DAYS`) and adds the `Global options` and `Help` sections the preview left out; the argument column aligns with the option labels.
## Decisions Made
See `key-decisions`. The two worth a look: the post-release fix above, and testing the production question on a real pseudo-terminal (`/dev/ptmx` with `syscall` ioctls, linux build tag), which needs no new dependency and no bonfire test seam.
## Deviations from Plan
### Auto-fixed Issues
**1. [Rule 1 - Bug] PeekGlobals ignored clustered shorthands**
- **Found during:** Task 3 (`TestGlobalFlagsAgree`)
- **Fix:** read single-dash clusters letter by letter; stop at the first non-global letter
- **Files:** modules/bonfire/globals.go
- **Commit:** `efb043e` (in the Post-release fixes table)
**2. [Rule 3 - Blocking] No injectable build-info reader**
- **Found during:** Task 3 (`TestVersionHeader`)
- **Fix:** `var readBuildInfo = debug.ReadBuildInfo`, used by `BuildVersion`
- **Commit:** `9641a1b` (refactor, no behaviour change)
**3. [Rule 3 - Blocking] gate build of library-only plugins**
- `go build -o dir/ ./...` refuses repositories without a main package; the plugins stage uses `GOWORK=off go build ./...`.
### Recorded differences
4. **Extra test files** not in the plan's list: `modules/conga/confirm_linux_test.go` (queue:clear's question, `TestQueueClearConfirm`), `modules/lagoon/pty_linux_test.go` (`TestConfirmProductionInteractiveNo`, `TestForceSkipsConfirm`), additional tests `TestDefaultConfig`, `TestConsoleLevelFromOptions`, `TestRouteListJSON` (in summary_test.go), `TestMigrateStatusJSON` (in pending_test.go, Docker).
5. **Component names with dots.** `TestConfigRules` pins the nested form as refused and the literal YAML key as accepted (compass keeps it literal); the docs' "cannot contain dots" wording is a deferred item.
6. **Version header.** Equal `devel` versions collapse to `SummerCMS acme devel`; pinned, deferred.
7. **TestErrorScrubbed** covers the credential shapes `sunscreen.Scrub` knows (Bearer, `sk-`, `x-api-key:`) in the message, explanation, Try line and `-v` chain; a `password=` in a message is not scrubbed (sunscreen contract; deferred).
8. **Widget matrix on a terminal.** Live views on a buffer draw no frames (no window size), so the terminal row checks the live path (three cursor-hide sequences, no fallback lines); `TestLiveModels` renders the frames with a fixed size and a pty run showed every final frame.
9. **TestServeGracefulNil and TestShutdownReportAborted** drive `requestCounter` and `shutdownReport` through a real `http.Server` with serve's drain logic; serve's own wiring of them is covered by `TestServeLifecycle` (Docker).
10. **Landing.** The stage runs `nuxt generate` before the TAP suite (plan 03 deviation 12); `TestTerminalCommands` is opt-in (NOT RUN line); `sync-roadmap.sh --check` uses `ROADMAP_REF=origin/master` because local master carries other sessions' unpushed planning commits.
11. **Verify chain flake.** One run of Task 3's verify chain stopped red after its race step without a visible failure; every piece passed separately and the full chain passed on the rerun.
**Total deviations:** 3 auto-fixed (1 bug, 2 blocking), 8 recorded differences.
## Issues Encountered
- Pre-existing gofmt drift in plan 03 files (`cmd/summer/main.go`, `runtime.go`, `runtime_test.go`, `modules/flare/commands.go`); logged in deferred-items.md.
## Known Stubs
None.
## Threat Flags
None. No new endpoint or input path; the goldens hold fixed fake values (secret scan over `modules/bonfire/testdata` finds no JWT, `sk-` key or private key header).
## g15office
No g15office tool is available in this executor session: the Phase 19.8 milestone task was not commented. Suggested comment once the Task 5 reply is in: "Plan 19.8-04 executed: phase gate green (--all with Docker), one post-release fix (PeekGlobals clustered shorthands); waiting on verification."
## Task 6: published
- **Range check** (after `git fetch`): `origin/master..v0.4.1` held 30 commits, all `.planning/` documents (this plan's and the 19.6, 19.6.1 and 19.7 sessions', `c1f5ded` included, approved with the reply) or this plan's code commits; no other session's code. `go vet ./...` and `go test -short ./...` green before tagging.
- **Tag:** annotated **v0.4.1** (tag object `520d0c7`) on `8bacc22` (master tip when tagged: another session's `docs(19.6.1): map phase to its g15office task` landed after the reply commit `2ca68f8`; it is planning-only and inside the approved range). Contains `efb043e` and every test commit of this plan.
- **Pushed:** framework `master` `99b6df4..8bacc22` (pushed as the exact tagged commit, `8bacc22:refs/heads/master`), then the tag. Nothing force-pushed. `git ls-remote --exit-code --tags origin v0.4.1` exits 0.
- **Changelog:** https://summercms.io/changelog/summercms-0-4-1 (post id 36, published_at the tag date 2026-10-21T22:15:15Z), notes under `## Fixed` and `## Changed`, no planning IDs; the token was read from its file and never printed.
- **Plugins and applications:** nothing to publish (no require bumps, no regenerate commits; the table needs none).
- This SUMMARY's finalising commit is made after the push and stays local until the next push.
### Gate after the release
`PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all` at `8bacc22`:
```
PASS self-test
PASS go
PASS hello
PASS docs
PASS plugins
PASS apps
NOT RUN TestTerminalCommands (the deployed one-liner end to end: needs network, Docker and ports 54320 and 8080; set SUMMERCMS_TERMINAL_CHECK=1)
FAIL landing
```
`landing` fails on one check only: `sync-roadmap: plugins/demo/content/ROADMAP.md differs from origin/master`. The push published the other sessions' ROADMAP.md changes (19.6.1 inserted, 19.6 and 19.7 updates: 42 insertions, 6 deletions), so sm-demo-app's roadmap snapshot is now one sync behind origin, as deferred-items.md (plan 03) predicted ("re-sync after those commits are pushed"). Every other landing check passed in the same run (nuxt generate, 38/38 TAP, the terminal and roadmap Go tests), and with `ROADMAP_REF=99b6df4` (the previous origin/master) the landing stage passes. `--all` stops at the first red stage, so the docker stage was run on its own afterwards: **PASS** (TestServeLifecycle, the five lagoon checklist tests, the three cabana token JSON tests).
**Open for the user:** in sm-demo-app run `scripts/sync-roadmap.sh` and commit the refreshed `plugins/demo/content/ROADMAP.md` (a sibling-repository commit, not covered by the release reply, so not made here). After that `--all` is fully green.
## Next
Re-sync the demo roadmap snapshot (above), then `/msd-verify-work 19.8`.
## Self-Check: PASSED
Every created file listed above exists (9 goldens), and the eleven commits `9b2fa6c`, `24f4643`, `db83a14`, `efb043e`, `9641a1b`, `7b58ad5`, `eeeee59`, `fd52868`, `6720cae`, `2baec56`, `72b8383` resolve.

View File

@@ -0,0 +1,456 @@
---
phase: 19.8-console-commands-overhaul
plan: 05
type: execute
wave: 1
depends_on: []
gap_closure: true
files_modified:
- modules/bonfire/live.go
- modules/bonfire/output.go
- modules/bonfire/widgets.go
- modules/bonfire/helpers.go
- modules/bonfire/errors.go
- modules/bonfire/execute.go
- modules/bonfire/README.md
- modules/bonfire/live_test.go
- modules/bonfire/live_signal_unix_test.go
- modules/bonfire/widgets_test.go
- modules/bonfire/errors_test.go
- modules/diary/daily.go
- modules/diary/diary.go
- modules/diary/console.go
- modules/diary/README.md
- modules/diary/daily_test.go
- modules/diary/diary_test.go
- modules/lagoon/migrations.go
- modules/lagoon/commands.go
- modules/lagoon/README.md
- modules/lagoon/pending_test.go
- cmd/summer/runtime.go
- cmd/summer/runtime_test.go
- internal/dev/watch.go
- internal/dev/watch_test.go
- docs/console/introduction.md
- docs/console/writing-commands.md
- docs/console/scaffolding.md
- docs/services/logging.md
- docs/setup/upgrading.md
- scripts/check-phase19.8.sh
- .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
- .planning/todos/pending/2026-10-22-console-review-info-findings.md
- .planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md
autonomous: true
requirements: [D-03, D-04, D-10, D-12, D-18, D-19, D-21, D-25, D-27, D-28, D-29]
user_setup: []
estimate:
tokens: 200000
raw_tokens: 200000
tasks: 7
confidence: low
must_haves:
truths:
- "CR-01 (D-03, D-27): lines printed while a Spinner, Progress or Process view runs (Output.Println and Printf, status lines, Progress Fail lines, bonfire.PrintAbove from diary) appear in the order they were printed and none is lost when the view ends; a line printed from another goroutine while the view finishes appears once, after the final frame (TestPrintAboveOrderedNoLoss)."
- "WR-02 (D-03): only the console that started a live view sends its Println, Printf and status lines above it; every other console (a bonfire.Call buffer, a nested command, a console on a file) writes to its own writer. At most one view registers at a time (check and set under one lock; a widget that loses falls back to plain lines), and a panic in the work function still unregisters, finishes and waits for the view (TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters)."
- "WR-01 (D-03): SIGINT or SIGTERM during a live view kills the view (cursor and terminal restored) and then still ends the process with the signal's default action, or reaches the command's own signal handler when it has one. The generated main and bonfire.Execute's context are unchanged (TestLiveViewRestoresTerminalOnSignal)."
- "CR-02 (D-18): `summer <delegate>` exits with the app binary's exit code (an exit 2 from the binary stays 2) and prints no second error block; a binary killed by a signal still gets summer's error block and exit 1. The carrier is the additive bonfire.WithExitCode, honoured by bonfire.Execute (TestDelegateExitCodePassthrough, TestWithExitCode)."
- "CR-03 (D-27, D-28, D-04): on a terminal the delegated binary gets summer's own stdout and stderr (so its production question, styled summaries and widgets work); off a terminal its stdout goes through summer's Output and its stderr to summer's stderr, never into stdout (TestDelegateStreams, TestAppStreams)."
- "WR-03 (D-25, D-29): summer dev sends serve SIGTERM and waits the same grace as the delegates (longer than serve's 10-second drain) before it kills it (TestDevStopWaitsForGracefulShutdown)."
- "WR-04 (D-10): a daily file that fails to open is skipped and retried one minute later by the writer's clock, or at the first record of a new date, whichever comes first; the warning is logged once per date (TestDailyRetriesAfterFailure; TestDailyUnwritable and TestDailyDegradesSmoke stay green)."
- "WR-06 (D-10): daily writers replaced by Setup, Bootstrap, ApplyGlobals or Reset are retired: a late record through them is appended and the file closed again, so no descriptor stays open; diary.Close keeps its documented reopen-on-next-record behaviour for the installed writers. ApplyGlobals holds the diary lock for its whole run, so concurrent calls rebuild the channels once (TestRetiredDailyWriterKeepsNoFile, TestApplyGlobalsRebuildsOnce)."
- "WR-05 (D-12, D-18): error blocks (message, explanation, Try lines, -v chain), process-view item names and errors, and table cells strip control characters other than tab before styling, through the additive bonfire.Sanitize, which diary's console handler also uses (TestRenderErrorStripsControl, TestProcessViewStripsControl, TestTableStripsControl, TestSanitize; TestConsoleStripsControlChars stays green)."
- "WR-07 (D-21, D-04): Migrate applies exactly the sets migrationSets lists (one list for Migrate, PendingMigrations, --pretend, the production question, the startup summary and migrate:status --json), keeping each set's error wording; migrate's 'N applied' lines and its result line count what Migrate actually applied, also when it fails partway (TestMigrateAppliedLines, Docker)."
- "WR-08 (D-19): the wrong-directory Try line keeps the command and its other arguments but replaces the value of every flag whose name contains password, passwd, secret, token or key with ***, in both the --flag value and --flag=value forms (TestWrongDirectoryMasksSecrets)."
- "Each CR and WR finding is fixed in its own commit that contains its pinning test, and replaying the commit without its non-test, non-doc changes makes the test fail (RED replay in Task 7). 19.8-REVIEW-DISPOSITION.md marks CR-01..CR-03 and WR-01..WR-08 fixed with their commit; IN-01..IN-09 are deferred to one pending todo (severity minor, area framework)."
- "No exported identifier of the framework is removed or changes its signature; the only API additions are bonfire.WithExitCode and bonfire.Sanitize. The generated main is unchanged, so no shared plugin needs a require bump and no application a regenerate commit unless the SUMMARY's Release impact table says otherwise."
artifacts:
- path: "modules/bonfire/live.go"
provides: "ordered, drained printing above live views; owner-checked registration; signal-safe teardown"
contains: "func runLive("
- path: "modules/bonfire/live_test.go"
provides: "CR-01 and WR-02 pinning tests"
contains: "TestPrintAboveOrderedNoLoss"
- path: "modules/bonfire/live_signal_unix_test.go"
provides: "WR-01 pinning test through a re-executed helper process"
contains: "TestLiveViewRestoresTerminalOnSignal"
- path: "modules/bonfire/errors.go"
provides: "bonfire.WithExitCode, bonfire.Sanitize in the error block, masked wrong-directory hint"
contains: "func WithExitCode("
- path: "cmd/summer/runtime.go"
provides: "delegates with pass-through exit codes and separate streams"
contains: "WithExitCode"
- path: "modules/diary/daily.go"
provides: "retry after a failed open; retired writers"
contains: "retire"
- path: "modules/lagoon/migrations.go"
provides: "Migrate over migrationSets"
contains: "migrationSets(gdb, plugins)"
- path: ".planning/todos/pending/2026-10-22-console-review-info-findings.md"
provides: "IN-01..IN-09 captured"
contains: "IN-09"
key_links:
- from: "modules/bonfire/output.go"
to: "modules/bonfire/live.go"
via: "Println, Printf and emit reroute above a view only when this console owns it"
pattern: "owner"
- from: "modules/diary/console.go"
to: "modules/bonfire/errors.go"
via: "diary's console sanitizer delegates to bonfire.Sanitize"
pattern: "bonfire.Sanitize"
- from: "cmd/summer/runtime.go"
to: "modules/bonfire/execute.go"
via: "runAppBinary wraps a child exit in Reported and WithExitCode; exitCode honours the carrier"
pattern: "WithExitCode"
- from: "modules/lagoon/migrations.go"
to: "modules/lagoon/commands.go"
via: "migrate reports pending-before minus pending-after as its applied lines"
pattern: "PendingMigrations"
- from: "scripts/check-phase19.8.sh"
to: "modules/bonfire/live_test.go"
via: "the go stage requires the pinning tests by name"
pattern: "TestPrintAboveOrderedNoLoss"
prohibitions:
- statement: "No exported framework identifier is removed, renamed or changes its signature (core shared plugins and applications build against them); additions only."
status: unresolved
verification: judgment
- statement: "WR-01 is fixed inside bonfire's live views: the generated main (internal/build), bonfire.Execute's context and Ctrl+C semantics of commands without a live view do not change."
status: unresolved
verification: judgment
- statement: "IN-01..IN-09 are not fixed in this plan; they go to the pending todo."
status: unresolved
verification: judgment
- statement: "No new Go module dependency; nothing is pushed or tagged by this plan (plan 06 owns the release)."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not in user-story format; plans 01-04 recorded the decision not to invent one): console commands in `summer` and in application binaries reach the Artisan bar; a user sees what a command did, can ask for more or less of it, and learns from `--help` how to use it.
This plan (gap closure 1 of 2, wave 1) closes the code review of the shipped console (19.8-REVIEW.md, v0.3.1..v0.4.1): the three critical findings CR-01..CR-03 and the eight warnings WR-01..WR-08. The nine info findings IN-01..IN-09 become one pending todo. Plan 06 releases the fixes as v0.4.2 under a go-ahead.
## Spec-less probe fallback
Spec-less probe fallback skipped: the phase has no REQUIREMENTS.md IDs (`Requirements: TBD`) and no SPEC.md, as in plans 01-04. `requirements` lists the CONTEXT decisions each finding breaks; `must_haves` derive from the review's findings.
## Decisions taken in this plan (Claude's discretion inside the review's suggested fixes)
- **Order of work.** The tracer (Task 1) is CR-01 alone: one finding taken through the whole loop that every later task repeats (pinning test red, fix, green, one commit). The review's "bonfire live view" group (CR-01, WR-01, WR-02) therefore spans Tasks 1 and 2.
- **CR-01 mechanism.** Each PrintAbove call becomes one print message placed straight on the program's message queue by the calling goroutine (the bubbletea event loop prints such a message itself, in queue order), instead of a model reply that bubbletea runs as a command on its own goroutine. Unregistering, sending the finish message and waiting for the program happen as one step under the registry lock, so no line reaches a stopped program and no line races the final frame. The review's alternative (a FIFO in the model drained with `tea.Sequence` before quitting) is the fallback if bubbletea v2.1.0's source does not support the first approach.
- **WR-01 mechanism.** The review offers a process-wide `signal.NotifyContext` around Execute or a handler inside `runLive`. This plan takes the handler inside `runLive`: a process-wide context would change Ctrl+C for every plugin command that ignores its context (the process would no longer stop) and would change the generated main, forcing regenerate commits in every application.
- **CR-02 carrier.** An explicit, additive `bonfire.WithExitCode(err, code)`; bonfire does not honour every error that happens to have an `ExitCode()` method, so plugin commands that return an `*exec.ExitError` keep exiting 1.
- **WR-05 home.** `bonfire.Sanitize`, as the review suggests; diary's console sanitizer delegates to it so one implementation exists. IN-08 (raw C1 bytes) stays in the todo and now points at `bonfire.Sanitize`.
- **WR-04 retry.** Retry after one minute (writer's clock) or on a new date, whichever comes first; one warning per date. The existing warning message `daily log file disabled` is kept so the pinned tests and any log alerts keep matching.
- **WR-06 retired writers.** A retired writer appends a late record and closes the file again (open, write, close), so neither a record nor a descriptor is lost. `diary.Close` keeps its documented behaviour (installed writers reopen on the next record).
- **WR-07 applied lines.** migrate computes pending migrations before and after Migrate and prints the difference, also when Migrate fails partway (then it prints what was applied and returns the error).
- **Review line numbers.** The review's line numbers for `modules/diary/daily.go` and `modules/bonfire/execute.go` do not match the source (the code is unchanged since v0.4.1); every task names the symbols to locate.
## Artifacts this plan produces
- Fix commits (one per finding, each with its pinning test) in bonfire, cmd/summer, internal/dev, diary and lagoon, with README and docs updates in the same commit where documented behaviour or exported API changes.
- `scripts/check-phase19.8.sh` requiring the new tests by name.
- `19.8-REVIEW-DISPOSITION.md` updated; `.planning/todos/pending/2026-10-22-console-review-info-findings.md`.
- `19.8-05-SUMMARY.md` with the RED evidence per finding, the Release impact table and `plan_head_before`/`plan_head_after`.
<objective>
Fix the eleven critical and warning findings of the 19.8 code review, each in its own commit with a test that fails without the fix, without breaking any exported API, and capture the nine info findings as a todo.
Purpose: the console overhaul shipped in v0.4.0/v0.4.1 loses and reorders output above live views, hides the terminal from summer's delegates, misreports their exit codes, leaves the cursor hidden after Ctrl+C, drops the daily log trail after one failed open and prints untrusted escape sequences; D-03, D-10, D-12, D-18, D-19, D-21, D-25, D-27, D-28 and D-29 promise otherwise.
Output: eleven fix commits, one gate commit, one planning-docs commit, the SUMMARY with a Release impact table for plan 06.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.8-console-commands-overhaul/19.8-REVIEW.md
@.planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
@.planning/phases/19.8-console-commands-overhaul/19.8-CONTEXT.md
@.planning/phases/19.8-console-commands-overhaul/19.8-04-SUMMARY.md
@.planning/phases/19.8-console-commands-overhaul/deferred-items.md
Repos and concurrency: summercms.go only. Several sessions work in this repository: before each task run `git status --porcelain` on its paths, stage only those paths by name (never `git add -A`), never touch `.planning/STATE.md` or `.planning/state.json` by hand, no co-author tags, planning docs and code in separate commits. Record `git rev-parse HEAD` as `plan_head_before` in the SUMMARY before the first commit. `go vet ./...` and `go test -short ./...` green at every commit; `go -C examples/hello test ./...` green before the last commit.
Fix loop for every finding (the tracer proves it once): write the pinning test, run it and keep the failing output (RED) for the SUMMARY, implement the fix, run the test and its package green (with `-race` for bonfire and diary), update README and docs when exported API or documented behaviour changes, commit the test, the fix and the docs together as one `fix(<module>): ...` commit. The RED replay in Task 7 re-proves each RED from the commits.
Docker: Docker-backed tests need `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1`; if the socket is denied although the user is in the docker group, run the tests through `sudo -u golem`. A Docker test that could not run is NOT RUN in the SUMMARY, never PASS.
<interfaces>
- bonfire console seam: `newConsole(in, out, errW, lookup, tty, interactive *bool)` and `setGlobals(Globals)` (modules/bonfire/output.go); `boolPtr`, `noEnv` (widgets_test.go); a terminal console on a bytes.Buffer runs the live path and the buffer receives the PrintAbove lines (TestPrintAbove).
- bonfire live path: `runLive(ctx, out, c, model, work)`, `printAboveLines(s)`, `liveActive()`, `liveMu`, `activeProg`, `liveModel`, `printAboveMsg`, `finishMsg` (live.go); `console.live()`, `emit`, `Println`, `Printf` (output.go); `Spinner`, `Progress`, `liveProgress.Fail`, `Table` (widgets.go); `Process`, `PrintAbove` (helpers.go).
- bubbletea v2.1.0 (path: `go list -m -f '{{.Dir}}' charm.land/bubbletea/v2`): tea.go `handleCommands` (each command on its own goroutine), the event loop's `case printLineMessage` (prints in the loop), `Program.Send` (returns once the program has stopped), `Program.Kill` (restores the terminal), `execSequenceMsg`; renderer.go `Println`.
- bonfire errors: `RenderError`, `wrongDirectory(name, err, args, dir)`, `shellQuote`, `Reported`, `ErrReported`, `isUsage` (errors.go); `Execute`, `exitCode`, `Fail` (execute.go).
- summer delegates: `appRunner`, `delegateCommand`, `runAppBinary`, `newAppCmd`, `runChild`, `appShutdownGrace` (cmd/summer/runtime.go); test helpers `captureAppRunner`, `runSummer`, `TestSignalHelperProcess`, `helperArgv`, `helperEnv`, `syncBuffer`, `waitForOutput` (cmd/summer/runtime_test.go).
- summer dev: `Options{Build, Start, Debounce}`, `watch`, `child.stop`, `startBin` (internal/dev/watch.go); serve's drain timeout is the 10-second `context.WithTimeout` in modules/surf/serve.go.
- diary: `dailyWriter{mu, disabled, file, date, now, warn}`, `Write`, `open`, `Close`, `newDailyWriter` (daily.go); `install`, `SetVerbosity`, `ApplyGlobals`, `Close`, `closeChannels`, `Reset`, `mu`, `current`, `gens` (diary.go); `channelBuilder.warn` with message `daily log file disabled` (channels.go); `sanitize` (console.go); `Options.Now`.
- lagoon: `Migrate`, `migrator`, `migrationSet`, `migrationSets`, `PendingMigrations` (migrations.go); the `migrate` command's Run (commands.go); Docker helpers `pendingPlugins`, `migPlugin`, `runLagoon` (pending_test.go).
</interfaces>
</context>
<tasks>
<task type="tracer" tdd="true">
<name>Task 1: Tracer: CR-01 lines printed above a live view keep their order and none is lost when the view ends (one fix through the whole loop: red test, fix, green, one commit)</name>
<files>modules/bonfire/live.go, modules/bonfire/widgets.go, modules/bonfire/output.go, modules/bonfire/helpers.go, modules/bonfire/live_test.go</files>
<behavior>
- TestPrintAboveOrderedNoLoss, subtests spinner, progress and process: on a terminal console over a bytes.Buffer (newConsole with tty and interactive true), 20 runs per widget; in each run the work function prints 100 numbered lines (`line-000` .. `line-099`, mixing Output.Println, Output.Printf with a trailing newline, Output.Info and bonfire.PrintAbove), the progress subtest also records two failures through bonfire.Failed between them, and the last line is printed immediately before the work function returns. After the widget returns, the ANSI-stripped buffer holds every numbered line exactly once and in order, and both failure lines.
- Subtest concurrent_finish: a goroutine started inside the work function keeps calling bonfire.PrintAbove after work returns; each of its lines appears exactly once in the buffer (above the view or after its final frame), none is lost.
- TestPrintAbove, TestWidgetMatrix and TestLiveModels stay green.
</behavior>
<read_first>modules/bonfire/live.go (whole file), modules/bonfire/widgets.go (Spinner, Progress, liveProgress), modules/bonfire/output.go (emit, Println, Printf, live), modules/bonfire/helpers.go (Process, PrintAbove), modules/bonfire/widgets_test.go (TestPrintAbove and the console helpers), modules/bonfire/live_test.go, the bubbletea v2.1.0 sources named in the interfaces block, 19.8-REVIEW.md (CR-01)</read_first>
<action>Per CR-01 (D-03, D-27). Verify the review's claim against bubbletea v2.1.0's source before changing anything: `liveModel.Update` answers each `printAboveMsg` with `tea.Println(...)`, a command that `handleCommands` runs on its own goroutine, so successive lines race back into the event loop and a line still in flight when `finishMsg` quits the program is dropped.
Write TestPrintAboveOrderedNoLoss in modules/bonfire/live_test.go and run it on the unchanged code; keep the failing output for the SUMMARY (out-of-order or missing lines). If it does not fail in three runs, raise the runs or the line count until it does, and record the numbers.
Fix in live.go (and where the callers send): every PrintAbove call (printAboveLines, and liveProgress.Fail's failure line) becomes ONE print message for the whole block, placed directly on the program's message queue by the calling goroutine in call order, so the event loop prints it itself without a command goroutine (for example send the message value that `tea.Println(block)` returns, through `Program.Send`, which also returns instead of blocking once the program has stopped; never call `Program.Println`, which blocks forever on a stopped program). Then make the end of runLive one step under `liveMu`: clear the registration, send `finishMsg` and wait for the program to finish while holding the lock, so a printer that arrives meanwhile waits and then writes directly after the final frame. Remove `printAboveMsg` handling from `liveModel` if nothing sends it any more. If the source shows the direct-queue approach does not hold, implement the review's alternative instead (a FIFO of pending lines in the model, drained with `tea.Sequence(<prints>..., tea.Quit)` on `finishMsg`) and record why.
No exported signature changes (bonfire.PrintAbove keeps its signature and meaning). The README's PrintAbove row already describes the intended behaviour; no docs change unless a sentence there contradicts the fix. Commit test and fix together: `fix(bonfire): keep the order of lines printed above a live view and drain them before it ends`.</action>
<verify>
<automated>go test -count=1 -race -short -run '^(TestPrintAboveOrderedNoLoss|TestPrintAbove|TestWidgetMatrix|TestLiveModels)$' -v ./modules/bonfire 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 4 &amp;&amp; go vet ./... &amp;&amp; go test -count=1 -short ./...</automated>
<fails_when>CR-01: fewer than 4 top-level "--- PASS: Test" lines (a numbered line missing, duplicated or out of order in any of the 60 widget runs, a failure line missing, a concurrent_finish line lost or printed twice, or a race report), or vet or the short suite fail.</fails_when>
</verify>
<acceptance_criteria>
- CR-01: `go test -count=3 -race -short -run '^TestPrintAboveOrderedNoLoss$' ./modules/bonfire` exits 0 (three clean passes in a row).
- CR-01: the SUMMARY records the RED output of TestPrintAboveOrderedNoLoss on the code before the fix (the first out-of-order or missing line).
- The CR-01 fix commit (found by its subject in `git log --format='%h %s' <plan_head_before>..HEAD`) lists modules/bonfire/live_test.go together with the changed non-test files in `git show --stat`.
</acceptance_criteria>
<done>Output printed during any live view arrives complete and in order, proven by a deterministic buffer test that fails on the shipped code, in one commit.</done>
</task>
<task type="auto" tdd="true">
<name>Task 2: WR-02 then WR-01: a live view takes lines only from the console that owns it, registers by check and set and unregisters on panic; Ctrl+C or SIGTERM during a view restores the terminal and keeps the signal's effect</name>
<files>modules/bonfire/live.go, modules/bonfire/output.go, modules/bonfire/widgets.go, modules/bonfire/helpers.go, modules/bonfire/live_test.go, modules/bonfire/live_signal_unix_test.go, modules/bonfire/README.md</files>
<behavior>
- TestPrintlnStaysWithItsConsole: while a Spinner runs on terminal console A (buffer A), console B (a non-terminal console over buffer B) calls Println, Printf and Info; buffer B holds all three and buffer A none of them; console A's own Println still prints above its view.
- TestLiveViewSingleOwner: two terminal consoles start a Spinner at the same moment (a barrier, 200 rounds); in every round exactly one runs as a live view and the other prints its plain `[...] <label>` line; after each round bonfire.PrintAbove reports false.
- TestLiveViewPanicUnregisters: a Spinner whose work panics; the test recovers the panic; afterwards bonfire.PrintAbove reports false and a new Spinner runs as a live view.
- TestLiveViewRestoresTerminalOnSignal (unix build tag, the test binary re-executed as a helper with an environment switch, like cmd/summer's TestSignalHelperProcess): the helper runs a Spinner on a terminal console over its stdout pipe, prints `ready` on stderr and blocks; the test sends SIGINT. Subtest default: the helper dies by SIGINT (default action kept) and its stdout holds the show-cursor sequence after the last hide-cursor sequence. Subtest own_handler: the helper also holds its own signal.NotifyContext, exits 0 by itself after its context ends, and its stdout holds the show-cursor sequence after the last hide-cursor sequence.
</behavior>
<read_first>modules/bonfire/live.go and output.go after Task 1, modules/bonfire/widgets.go, modules/bonfire/helpers.go (Process passes its ctx), cmd/summer/runtime_test.go (TestSignalHelperProcess, helperArgv, helperEnv, waitForOutput: the re-exec helper pattern), modules/lagoon/pty_linux_test.go (the syscall-only pseudo-terminal helper, only if a pipe does not show the cursor sequences), internal/build/build.go (the generated main: must stay unchanged), 19.8-REVIEW.md (WR-01, WR-02)</read_first>
<action>Two findings, two commits, WR-02 first because WR-01 builds on the registration it reworks.
WR-02 (D-03): verify against the source that `console.Println`, `console.Printf` and `emit` reroute through the single process-wide `activeProg` without checking which console started it, that `console.live()`'s `liveActive()` check and runLive's registration are separate steps, and that runLive unregisters without `defer`. Write the three WR-02 tests and record their RED output (the single-owner test may need more rounds to fail; if it does not fail in three runs, add an unexported test hook called between the check and the registration, nil in production, to force the interleaving, and record that). Fix: keep the owning console with the registered program; Println, Printf and emit reroute only when the calling console is the owner (bonfire.PrintAbove, used by diary, keeps rerouting to whichever view runs, as documented). Replace the separate check with a check-and-set claim under `liveMu` taken before the program starts; a widget that cannot claim prints its plain lines as nested widgets do today. Unregister, finish and wait in a deferred step (keeping Task 1's single locked step), so a panic in work leaves no orphaned registration and still re-panics. Commit: `fix(bonfire): reroute only the owning console's lines to a live view`.
WR-01 (D-03): verify that the live views run with bubbletea's own signal handling off, that Spinner and Progress pass a background context and that neither summer nor the generated main installs a signal handler, so SIGINT's default action ends the process with the cursor hidden. Write TestLiveViewRestoresTerminalOnSignal and record its RED output (no show-cursor after the last hide-cursor). First confirm on the unchanged code that the helper's stdout pipe carries the hide-cursor sequence; if a pipe never carries the cursor sequences, open a pseudo-terminal for the helper's stdout with a copy of the syscall-only helper from modules/lagoon/pty_linux_test.go (linux build tag, no new dependency). Fix inside runLive only: for the life of the view, relay SIGINT and SIGTERM to a channel with signal.Notify (skip a signal that signal.Ignored reports); on a signal, kill the program (Program.Kill restores the cursor and the terminal), wait for it to stop, clear the registration if it is still this view, stop relaying (signal.Stop), then re-raise the same signal on the own process (os.FindProcess(os.Getpid()) then Signal; if that fails, exit with 128 plus the signal number) so the default action, or the command's own registered handler, still applies. When the view ends normally, stop relaying before runLive returns. Do not change internal/build's generated main, bonfire.Execute's context or the summer main. README: the Spinner, Progress and Process rows or the widgets paragraph say that Ctrl+C during a view restores the terminal and then stops the command as before. Commit: `fix(bonfire): restore the terminal when a live view is interrupted`.</action>
<verify>
<automated>go test -count=1 -race -short -run '^(TestPrintlnStaysWithItsConsole|TestLiveViewSingleOwner|TestLiveViewPanicUnregisters|TestLiveViewRestoresTerminalOnSignal|TestPrintAboveOrderedNoLoss|TestPrintAbove|TestWidgetMatrix)$' -v ./modules/bonfire 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 7</automated>
<fails_when>WR-02: TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner or TestLiveViewPanicUnregisters does not pass (a line lands in another console's view, two views run at once, or a panic leaves a registration behind). WR-01: TestLiveViewRestoresTerminalOnSignal does not pass (the cursor is left hidden, the default signal action is lost, or the command's own handler no longer receives the signal). Any race report, or Task 1's tests regress.</fails_when>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; F=$(git log --format= --name-only --grep='^fix(bonfire): restore the terminal when a live view is interrupted$' --grep='^fix(bonfire): reroute only the owning console' -- internal/build cmd/summer/main.go) &amp;&amp; test -z "$F"</automated>
<fails_when>vet or the short suite fail, or either fix commit touches the generated main (internal/build) or summer's main.</fails_when>
</verify>
<acceptance_criteria>
- WR-02: `go test -count=3 -race -short -run '^(TestPrintlnStaysWithItsConsole|TestLiveViewSingleOwner|TestLiveViewPanicUnregisters)$' ./modules/bonfire` exits 0, and the SUMMARY records each test's RED output.
- WR-01: `go test -count=1 -short -run '^TestLiveViewRestoresTerminalOnSignal$' -v ./modules/bonfire 2>&1 | grep -cE -- '--- PASS: TestLiveViewRestoresTerminalOnSignal/(default|own_handler)'` prints 2, and the SUMMARY records the RED output.
- `git log --format=%s <plan_head_before>..HEAD` lists the WR-02 and WR-01 fix subjects as two separate commits (other sessions' commits may sit between them), and `git show --stat` of each lists its test file.
</acceptance_criteria>
<done>A live view belongs to one console, cannot be registered twice or left registered, and an interrupted view gives the terminal back without changing how the signal ends the command.</done>
</task>
<task type="auto" tdd="true">
<name>Task 3: summer delegates and dev: CR-02 exit codes pass through without a second error block, CR-03 the binary gets the terminal and a separate stderr, WR-03 dev waits serve's full drain, WR-08 the wrong-directory hint masks secret flag values</name>
<files>cmd/summer/runtime.go, cmd/summer/runtime_test.go, modules/bonfire/execute.go, modules/bonfire/errors.go, modules/bonfire/errors_test.go, modules/bonfire/README.md, internal/dev/watch.go, internal/dev/watch_test.go, docs/console/introduction.md, docs/console/writing-commands.md, docs/console/scaffolding.md, docs/setup/upgrading.md</files>
<behavior>
- TestWithExitCode (bonfire): bonfire.Execute returns 2 for a command returning Reported(WithExitCode(err, 2)) and prints no error block; WithExitCode(err, 3) without Reported prints the block and returns 3; codes below 1 or above 255 return ExitFailure; WithExitCode(nil, 2) is nil; errors.Is and errors.As still see the wrapped error.
- TestDelegateExitCodePassthrough (cmd/summer): a summer root runs a delegate whose child (the test binary re-executed as a helper) writes a line on stderr and exits 2; Execute returns 2 and summer's own output and error streams hold no ERROR block and no `summer: migrate` text. A child killed by SIGKILL gives exit 1 and one summer error block naming the signal.
- TestAppStreams (cmd/summer): the stream choice for a terminal Output returns os.Stdout and os.Stderr; for a non-terminal Output it returns that Output and os.Stderr.
- TestDelegateStreams (cmd/summer): with a non-terminal buffer console, a helper child writing `data-line` to stdout and `log-line` to stderr leaves `data-line` in the buffer and no `log-line` in it.
- TestDevStopWaitsForGracefulShutdown (internal/dev): a helper child that traps SIGTERM, sleeps 2.5 seconds and exits 0 is stopped through child.stop and reports exit 0 (not killed); dev's stop grace equals the delegates' grace and exceeds 10 seconds.
- TestWrongDirectoryMasksSecrets (bonfire): from a temp tree (application root with summer.yaml, cwd without config/), args `admin:create --email a@b.c --password hunter2`, `--password=hunter2`, `--client-secret s3cr3t`, `--api-token=t0k3n` and `--key k3y` give a Try line without hunter2, s3cr3t, t0k3n or k3y, with `--password ***` or `--password=***` in their place and `--email a@b.c` unchanged; TestWrongDirectoryHint stays green.
</behavior>
<read_first>cmd/summer/runtime.go (delegateCommand, runAppBinary, newAppCmd, runChild, appShutdownGrace), cmd/summer/runtime_test.go (captureAppRunner, runSummer, the signal helper), cmd/summer/main.go (how the root is built), modules/bonfire/execute.go (Execute, exitCode, Fail), modules/bonfire/errors.go (Reported, RenderError, wrongDirectory, shellQuote), modules/bonfire/errors_test.go and wrongdir_smoke_test.go (TestWrongDirectoryHint), internal/dev/watch.go (watch's stream choice, child.stop, startBin), internal/dev/watch_test.go (TestStartBinServe, TestDevGracefulNil), modules/surf/serve.go (the 10-second drain), docs/console/introduction.md ("Exit codes", "Commands that summer delegates"), docs/console/scaffolding.md (summer dev), docs/console/writing-commands.md (wrong-directory example), docs/setup/upgrading.md ("Upgrading to 0.4"), modules/bonfire/README.md (error block paragraph, API table), 19.8-REVIEW.md (CR-02, CR-03, WR-03, WR-08)</read_first>
<action>Four findings, four commits, in this order. Verify each claim against the source first and record the pinning test's RED output.
CR-02 (D-18): runAppBinary returns `summer: <argv>: exit status N` wrapping the child's *exec.ExitError, so Execute renders a second error block under the one the binary printed and maps it to exit 1. Add to bonfire an exported carrier `WithExitCode(err error, code int) error` (unexported type with Error, Unwrap; nil stays nil) and make `exitCode` honour it before the usage check (a code outside 1..255 maps to ExitFailure). In runAppBinary, when the child exited with a code (`ExitError.Exited()`), return `bonfire.Reported(bonfire.WithExitCode(<the wrapped error>, code))`; a child ended by a signal keeps today's error block and exit 1. Factor the part of runAppBinary after locating the binary into a function the tests can call with the test binary as the child. README: an API row for bonfire.WithExitCode and the Reported row amended ("exit code 1, or the code of bonfire.WithExitCode"); docs/console/introduction.md: the delegates section says summer exits with the binary's exit code; docs/setup/upgrading.md: one bullet under "Upgrading to 0.4" behaviour changes. Commit: `fix(summer): pass the app binary's exit code through and print its error once`.
CR-03 (D-27, D-28, D-04): newAppCmd sets the child's stdout and stderr to summer's Output, so the child never sees a terminal (no production question, no styled summary, no widgets) and its stderr lands in summer's stdout. Add a stream choice used by the delegates: on a terminal Output (bonfire.IsTerminal) the child gets os.Stdout and os.Stderr; otherwise stdout goes to the Output (keeps tests and Call roots capturing) and stderr to os.Stderr. newAppCmd takes the two writers (unexported; adjust its existing tests). The child keeps os.Stdin. Docs: the delegates section of docs/console/introduction.md says the binary writes to the terminal directly and its stderr stays separate, so `summer route:list --json > routes.json` captures only the JSON; extend the upgrading bullet. Commit: `fix(summer): give delegated commands the terminal and keep their stderr apart`.
WR-03 (D-25, D-29): child.stop in internal/dev/watch.go kills serve two seconds after SIGTERM while serve drains for up to ten. Move the grace into one constant shared by the delegates and dev: `ShutdownGrace` in internal/dev (15 seconds, with a comment naming serve's 10-second drain; internal, so not public API), used by child.stop, with cmd/summer's appShutdownGrace defined as `dev.ShutdownGrace`. The helper child for the test may be the test binary re-executed with an environment switch. docs/console/scaffolding.md: the summer dev paragraph says Ctrl+C waits for serve's shutdown report (up to its drain time) before stopping it. Commit: `fix(dev): give serve the full shutdown grace before killing it`.
WR-08 (D-19): wrongDirectory builds the Try line from every argument, so a `--password` value reaches stderr and CI logs. When building the Try line, replace the value of every long flag whose name (case-insensitive) contains password, passwd, secret, token or key with `***`, in the `--flag value` form (the next argument when it does not start with `-`) and the `--flag=value` form; write the mask without shell quoting; leave every other argument as today. README (error block paragraph) and docs/console/writing-commands.md (the wrong-directory paragraph) say secret-looking flag values are masked. Commit: `fix(bonfire): mask secret flag values in the wrong-directory hint`.
No other exported signature changes; bonfire.Reported, bonfire.Fail and bonfire.Execute keep their signatures.</action>
<verify>
<automated>go test -count=1 -race -short -run '^(TestWithExitCode|TestWrongDirectoryMasksSecrets|TestWrongDirectoryHint|TestExitCodes|TestReportedSilent|TestErrorGolden)$' -v ./modules/bonfire 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 6</automated>
<fails_when>CR-02 (bonfire side): TestWithExitCode fails (the carrier code is ignored, a block is printed for a Reported carrier, or an out-of-range code escapes). WR-08: TestWrongDirectoryMasksSecrets fails (a secret value appears in the Try line or another argument changed). Or TestWrongDirectoryHint, TestExitCodes, TestReportedSilent or the error goldens regress.</fails_when>
<automated>go test -count=1 -short -run '^(TestDelegateExitCodePassthrough|TestAppStreams|TestDelegateStreams|TestRunChildForwardsSignals|TestNewAppCmdCancelSendsSIGTERM|TestDelegateExamples)$' -v ./cmd/summer 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 6</automated>
<fails_when>CR-02: TestDelegateExitCodePassthrough fails (summer exits 1 for a child's 2, prints a second error block, or a signalled child is silent). CR-03: TestAppStreams or TestDelegateStreams fails (the child's stderr reaches summer's stdout, or a terminal Output does not hand the child the process's own streams). Or the existing delegate signal tests regress.</fails_when>
<automated>go test -count=1 -short -run '^(TestDevStopWaitsForGracefulShutdown|TestStartBinServe|TestDevReport|TestDevGracefulNil)$' -v ./internal/dev 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 4 &amp;&amp; go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>WR-03: TestDevStopWaitsForGracefulShutdown fails (serve is killed before its drain ends, or dev's grace differs from the delegates'). Or the dev tests, vet, the short suite or the docs checker (a README or docs identifier that does not exist) fail.</fails_when>
</verify>
<acceptance_criteria>
- CR-02: `grep -c '^func WithExitCode(' modules/bonfire/errors.go modules/bonfire/execute.go | awk -F: '{s+=$2} END {print s}'` prints 1 and `grep -c 'bonfire.WithExitCode' modules/bonfire/README.md` prints at least 1.
- CR-03: `grep -c 'os.Stderr' cmd/summer/runtime.go` prints at least 1 and TestDelegateStreams passes.
- WR-03: `grep -c 'dev.ShutdownGrace' cmd/summer/runtime.go` prints at least 1 and `grep -c 'ShutdownGrace' internal/dev/watch.go` prints at least 2 (declaration and use in child.stop); TestDevStopWaitsForGracefulShutdown passes.
- WR-08: TestWrongDirectoryMasksSecrets passes and `grep -ci 'masked\|\*\*\*' docs/console/writing-commands.md` prints at least 1.
- `git log --format=%s <plan_head_before>..HEAD` lists the four fix subjects as four separate commits; the SUMMARY records each pinning test's RED output.
</acceptance_criteria>
<done>summer's delegates behave like running the binary directly (terminal, separate streams, same exit code, one error block), summer dev lets serve finish its shutdown, and the wrong-directory hint never repeats a secret.</done>
</task>
<task type="auto" tdd="true">
<name>Task 4: diary: WR-04 a failed daily open is retried after a minute or on the next date, WR-06 replaced daily writers keep no descriptor and ApplyGlobals rebuilds once</name>
<files>modules/diary/daily.go, modules/diary/diary.go, modules/diary/daily_test.go, modules/diary/diary_test.go, modules/diary/README.md, docs/services/logging.md</files>
<behavior>
- TestDailyRetriesAfterFailure: with an injected clock at 2026-10-21 10:00:00 and a regular file where the log directory should be, the first record warns once (`daily log file disabled`); the test removes the blocking file; a record at 10:00:30 writes nothing and does not warn again; a record at 10:01:01 creates summer-2026-10-21.log holding it. A second scenario: the failure on 2026-10-21 23:59:59, blocker removed, a record at 2026-10-22 00:00:01 creates summer-2026-10-22.log; if the new date fails as well, exactly one more warning appears for it.
- TestRetiredDailyWriterKeepsNoFile: a daily writer that has written once is retired (as install and Reset do with the previous channels); a later record still lands in the day's file and afterwards the writer holds no open file; diary.Close on the installed writers still lets the next record reopen the file.
- TestApplyGlobalsRebuildsOnce: after Setup with color auto, 50 goroutines released by a barrier call ApplyGlobals with color never; the channels are rebuilt exactly once (the generation counter advances by one) and every call's verbosity is applied; run under -race.
- TestDailyUnwritable, TestDailyDegradesSmoke, TestDailyRotation, TestConcurrentWrites and TestResetRestoresDefault stay green.
</behavior>
<read_first>modules/diary/daily.go (dailyWriter, Write, open, Close), modules/diary/diary.go (install, SetVerbosity, ApplyGlobals, Close, closeChannels, Reset), modules/diary/channels.go (buildChannels, channelBuilder.warn), modules/diary/daily_test.go (TestDailyUnwritable, the setup helper, lockedBuffer, dailyOnly), modules/diary/diary_test.go, modules/diary/README.md (Daily files), docs/services/logging.md (the paragraph about a file that cannot be opened), 19.8-REVIEW.md (WR-04, WR-06), 19.8-01-PLAN.md threat T-19.8-06</read_first>
<action>Two findings, two commits. Locate the code by symbol (the review's daily.go line numbers do not match the file). Verify each claim and record the pinning tests' RED output.
WR-04 (D-10): dailyWriter.Write sets `disabled` on the first failed open and never clears it, so one transient failure (ENOSPC, EMFILE, a directory briefly removed at rotation) stops file logging until restart. Replace the permanent flag with a retry point: after a failed open, skip writes (still returning len(p), nil) until one minute has passed by the writer's clock or the local date changes, whichever comes first, then try to open again; warn through the existing warn function once per failed date (keep the message `daily log file disabled` so the pinned tests and existing alerts match). T-19.8-06's rule stands: boot never fails on a log file. Update the dailyWriter doc comment, modules/diary/README.md (Daily files bullet) and docs/services/logging.md (the cannot-be-opened paragraph: the channel skips records and tries again a minute later or on the next day, warning once per day). Commit: `fix(diary): retry a daily file that failed to open`.
WR-06 (D-10): install and Reset swap the state and then close the old channels' daily writers, but a goroutine holding the old state reopens the file through the closed writer and nothing closes it again; ApplyGlobals reads the state outside `mu`, so two concurrent calls can both rebuild. Add a retire step for replaced channels, separate from Close: a retired daily writer closes its file, and every later write opens the day's file, appends the record and closes it again (no record lost, no descriptor kept). install and Reset retire the old channels; the public diary.Close keeps closing the installed writers with reopen on the next record, as documented. Make ApplyGlobals hold `mu` for its whole run, using lock-free internal variants of install and SetVerbosity so nothing locks twice. Commit: `fix(diary): retire replaced daily writers and serialise ApplyGlobals`.
No exported signature changes in diary.</action>
<verify>
<automated>go test -count=1 -race -short -run '^(TestDailyRetriesAfterFailure|TestRetiredDailyWriterKeepsNoFile|TestApplyGlobalsRebuildsOnce|TestDailyUnwritable|TestDailyDegradesSmoke|TestDailyRotation|TestConcurrentWrites|TestResetRestoresDefault)$' -v ./modules/diary 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 8</automated>
<fails_when>WR-04: TestDailyRetriesAfterFailure fails (no retry after a minute or on a new date, a retry before the minute, or more than one warning per date). WR-06: TestRetiredDailyWriterKeepsNoFile or TestApplyGlobalsRebuildsOnce fails (a retired writer keeps a file open or loses the record, diary.Close no longer reopens, or concurrent ApplyGlobals calls rebuild more than once). Or a race report, or the existing daily, rotation, concurrency or reset tests regress.</fails_when>
<automated>go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>vet, the short suite or the docs checker fail.</fails_when>
</verify>
<acceptance_criteria>
- WR-04: `grep -ci 'next day\|a minute\|one minute' docs/services/logging.md` prints at least 1 and TestDailyRetriesAfterFailure passes.
- WR-06: `go test -count=3 -race -short -run '^(TestRetiredDailyWriterKeepsNoFile|TestApplyGlobalsRebuildsOnce)$' ./modules/diary` exits 0.
- `git log --format=%s <plan_head_before>..HEAD` lists the WR-04 and WR-06 fix subjects as two separate commits; the SUMMARY records each pinning test's RED output.
</acceptance_criteria>
<done>A long-running server keeps its file log trail through transient failures and setup changes, without leaking descriptors.</done>
</task>
<task type="auto" tdd="true">
<name>Task 5: WR-05 error blocks, process views and tables strip control characters through the additive bonfire.Sanitize, which diary's console handler also uses</name>
<files>modules/bonfire/errors.go, modules/bonfire/live.go, modules/bonfire/widgets.go, modules/bonfire/helpers.go, modules/bonfire/errors_test.go, modules/bonfire/live_test.go, modules/bonfire/widgets_test.go, modules/bonfire/README.md, modules/diary/console.go, docs/console/writing-commands.md</files>
<behavior>
- TestSanitize: tab kept, CR and LF become spaces, ESC, BEL, other C0 controls and DEL removed, clean strings returned unchanged; the same table as diary's TestConsoleStripsControlChars.
- TestRenderErrorStripsControl: with color forced on (Globals Color always), an error whose message, HintError explanation and Try text and wrapped cause (at -v) carry `\x1b]0;pwned\x07` and `\x1b[2J` renders a block that contains neither injected sequence (the palette's own styling sequences may remain) and still shows the visible text.
- TestProcessViewStripsControl: the process model's View, with an item name and an item error carrying the same sequences, contains neither.
- TestTableStripsControl: Table on a terminal console with color forced, and on a non-terminal console, with a cell and a header carrying the sequences, prints neither.
- TestConsoleStripsControlChars (diary) and the bonfire goldens stay green.
</behavior>
<read_first>modules/diary/console.go (sanitize and its callers), modules/bonfire/errors.go (RenderError, writeStyled, causes), modules/bonfire/live.go (processModel.View), modules/bonfire/widgets.go (Table, failureLine), modules/bonfire/helpers.go (Process, failure), modules/bonfire/errors_test.go, modules/bonfire/live_test.go, modules/bonfire/widgets_test.go, modules/diary/console_test.go (TestConsoleStripsControlChars), modules/bonfire/README.md, docs/console/writing-commands.md, 19.8-REVIEW.md (WR-05, IN-08), 19.8-01-PLAN.md threat T-19.8-03</read_first>
<action>Per WR-05 (D-12, D-18; extends threat T-19.8-03 from diary's console to bonfire's output). Verify against the source that RenderError passes text through sunscreen.Scrub only (which keeps ESC and other control characters), that processModel.View prints item names and `it.err.Error()` unchanged and that Table prints cells unchanged, and that the colorprofile writer passes such sequences through when color is on. Write the four tests with color forced on (the NoTTY profile strips all escape sequences and would hide the defect) and record their RED output.
Move diary's sanitizer into bonfire as the exported `Sanitize(s string) string` with the same rules (tab kept, CR and LF to a space, every other control character dropped) and make diary's console handler call it, deleting diary's copy. Apply it before styling to: every error-block line (message, explanation, Try lines and the -v chain, after splitting into lines and alongside sunscreen.Scrub), process-view item names and item errors, the failure lines of Progress and Process, and table headers and cells in both the terminal and the plain layout. Do not add the IN-08 change (invalid UTF-8 and raw C1 bytes); the todo records it against bonfire.Sanitize.
README: an API row for bonfire.Sanitize and one sentence in the error block and widgets text that printed text is stripped of control characters; docs/console/writing-commands.md: one sentence where error output and tables are described. Commit: `fix(bonfire): strip control characters from error blocks, process views and tables`.</action>
<verify>
<automated>go test -count=1 -race -short -run '^(TestSanitize|TestRenderErrorStripsControl|TestProcessViewStripsControl|TestTableStripsControl|TestErrorGolden|TestListGolden|TestWidgetMatrix|TestLiveModels)$' -v ./modules/bonfire 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 8 &amp;&amp; go test -count=1 -race -short -run '^(TestConsoleStripsControlChars|TestSlogtestConsoleHandler)$' ./modules/diary &amp;&amp; go vet ./... &amp;&amp; go test -count=1 -short ./... &amp;&amp; go run ./cmd/summer docs:build --check</automated>
<fails_when>WR-05: an injected OSC or CSI sequence survives in an error block, process view or table (TestRenderErrorStripsControl, TestProcessViewStripsControl, TestTableStripsControl), TestSanitize disagrees with diary's rules, diary's console tests regress, a golden changes, or vet, the short suite or the docs checker fail.</fails_when>
</verify>
<acceptance_criteria>
- WR-05: `grep -c '^func Sanitize(' modules/bonfire/*.go | awk -F: '{s+=$2} END {print s}'` prints 1, `grep -c 'bonfire.Sanitize' modules/diary/console.go` prints at least 1 and `grep -c 'bonfire.Sanitize' modules/bonfire/README.md` prints at least 1.
- `git log --format=%h <plan_head_before>..HEAD -- modules/bonfire/testdata` prints nothing (no golden changed by this plan).
- The SUMMARY records the RED output of the three WR-05 output tests.
</acceptance_criteria>
<done>Text from databases, remote services or admins can no longer move the cursor or retitle the operator's terminal through error blocks, process views or tables.</done>
</task>
<task type="auto" tdd="true">
<name>Task 6: WR-07 Migrate and PendingMigrations read one migration set list, and migrate's applied lines count what Migrate actually applied</name>
<files>modules/lagoon/migrations.go, modules/lagoon/commands.go, modules/lagoon/pending_test.go, modules/lagoon/README.md</files>
<precondition>A Docker daemon answers (`docker info`, directly or through `sudo -u golem`); TestMigrateAppliedLines is a testcontainers test. Without one, stop the task and report it.</precondition>
<behavior>
- TestMigrateAppliedLines (Docker, skipped under -short through the package's TestMain): two fixture plugins, alpha with one migration that succeeds and beta with one whose SQL fails; `migrate` on a fresh database prints `alpha: 1 applied` and the framework sets' applied lines, returns beta's error and prints no success line; after replacing beta's migration with a valid one, a second `migrate` prints `beta: 1 applied` and `Applied 1 migrations`, and a third prints `Nothing to migrate`.
- TestPendingMigrations, TestMigratePretend, TestMigrateStatusJSON, TestMigrateRunsSystemFilesFirst and TestTwoPluginMigrationSetsIsolated stay green.
</behavior>
<read_first>modules/lagoon/migrations.go (Migrate, migrator, migrationSet, migrationSets, PendingMigrations), modules/lagoon/commands.go (the migrate command's Run), modules/lagoon/pending_test.go (pendingPlugins, migPlugin, runLagoon, TestMain), modules/lagoon/migrations_test.go (TestMigrateRunsSystemFilesFirst), modules/lagoon/README.md, 19.8-REVIEW.md (WR-07)</read_first>
<action>Per WR-07 (D-21, D-04). Verify that Migrate lists the framework sets and the plugin loop by hand while PendingMigrations reads migrationSets, and that migrate prints its applied lines from the list computed before Migrate ran (so a failure partway prints none). Write TestMigrateAppliedLines and record its RED output (no applied lines when beta fails).
Make Migrate iterate `migrationSets(gdb, plugins)`, so the startup summary, --pretend, the production question's count, the applied lines and migrate:status --json read the same list. Give each set a label so Migrate's error wording stays as today (`lagoon: migrate system_files: ...`, `backend admin`, `deferred bindings`, `queue`, `<plugin id>`). In the migrate command, compute PendingMigrations before and after Migrate and print the per-set applied lines and the result line from the difference; when Migrate fails, print the lines for what was applied and then return its error. Migrate's signature and its order of sets do not change. README: the migrate description says the applied lines count what was applied, also when a migration fails. Commit: `fix(lagoon): one migration set list for Migrate and PendingMigrations`.</action>
<verify>
<automated>TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 -timeout 10m -run '^(TestMigrateAppliedLines|TestPendingMigrations|TestMigratePretend|TestMigrateStatusJSON|TestMigrateRunsSystemFilesFirst|TestTwoPluginMigrationSetsIsolated)$' -v ./modules/lagoon 2&gt;&amp;1 | grep -c -- '^--- PASS: Test' | grep -qx 6 &amp;&amp; go vet ./... &amp;&amp; go test -count=1 -short ./...</automated>
<fails_when>WR-07: TestMigrateAppliedLines fails (no applied lines on a partial failure, wrong counts, or a success line after a failure), any of the five existing migration tests regress, a test was skipped (fewer than 6 passes means Docker did not run: report NOT RUN, do not count it as passed), or vet or the short suite fail.</fails_when>
</verify>
<acceptance_criteria>
- WR-07: `grep -v '^\s*//' modules/lagoon/migrations.go | grep -c 'attach.Migrations'` prints 1 and `grep -v '^\s*//' modules/lagoon/migrations.go | grep -c 'QueueMigrations(sqlDB)'` prints 1 (the framework sets are listed once, in migrationSets).
- WR-07: `grep -c 'migrationSets(gdb, plugins)' modules/lagoon/migrations.go` prints at least 2 (Migrate and PendingMigrations).
- `git log --format=%s <plan_head_before>..HEAD` lists the WR-07 fix subject; the SUMMARY records the RED output.
</acceptance_criteria>
<done>A migration set added once is applied, counted, pretended and reported everywhere, and migrate reports what it really applied.</done>
</task>
<task type="auto">
<name>Task 7: The gate requires every pinning test, each fix is replayed red without its code, the full suites are green with Docker, the disposition record is updated and IN-01..IN-09 become one pending todo</name>
<files>scripts/check-phase19.8.sh, .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md, .planning/todos/pending/2026-10-22-console-review-info-findings.md, .planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md</files>
<read_first>scripts/check-phase19.8.sh (the required-name arrays and run_go, run_docker), 19.8-REVIEW-DISPOSITION.md (frontmatter and table format, the note on setting dispositions by hand), 19.8-REVIEW.md (IN-01..IN-09), .planning/todos/pending/2026-10-21-quiet-plugin-boot-logs-on-console-commands.md (todo format), this plan's commits (git log plan_head_before..HEAD)</read_first>
<action>(1) Gate. Add every new test name to the required arrays in scripts/check-phase19.8.sh with a `# Plan 05 (review fixes)` comment: BONFIRE_TESTS gets TestPrintAboveOrderedNoLoss, TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters, TestLiveViewRestoresTerminalOnSignal, TestWithExitCode, TestWrongDirectoryMasksSecrets, TestSanitize, TestRenderErrorStripsControl, TestProcessViewStripsControl, TestTableStripsControl; DIARY_TESTS gets TestDailyRetriesAfterFailure, TestRetiredDailyWriterKeepsNoFile, TestApplyGlobalsRebuildsOnce; SUMMER_TESTS gets TestDelegateExitCodePassthrough, TestAppStreams, TestDelegateStreams; DEV_TESTS gets TestDevStopWaitsForGracefulShutdown; DOCKER_LAGOON_TESTS gets TestMigrateAppliedLines. Use the names the tasks actually committed (record any rename). Leave IN-09's naming of consuming applications alone (it is in the todo). Commit: `test(19.8): gate requires the review fixes' tests`.
(2) RED replay. For each of the eleven fix commits C, in a temporary detached worktree under the session scratchpad (`git worktree add --detach <dir> C`), reverse-apply C's changes outside test files and Markdown (`git -C <dir> show C -- . ':!*_test.go' ':!*.md' > <dir>.patch && git -C <dir> apply -R <dir>.patch`), run C's pinning test by name and expect it to FAIL (a compile failure counts for the tests of the two new exported identifiers; say so in the table), then `git worktree remove --force <dir>`. The lagoon replay needs Docker. Record per finding: commit, pinning test, RED kind (assertion or compile), result. A pinning test that passes on the reverted code means the test does not pin the fix: stop and fix the test in its own `test(...)` commit before continuing.
(3) Full verification: `go vet ./...`; `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...` (through `sudo -u golem` if the socket is denied; the known wristband flake TestOIDCSubjectIsRandomSmoke from deferred-items.md may be rerun once and recorded); `go -C examples/hello test -count=1 ./...`; `bash scripts/check-phase19.8.sh go`; `bash scripts/check-phase19.8.sh docs`; `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh docker`. API check: no exported func, method or type declaration line removed between plan_head_before and HEAD in non-test Go files under modules/ unless the identical line is re-added (`comm -23` of the removed and added `func`/`type` lines, filtered to exported names, prints nothing).
(4) Records. Edit 19.8-REVIEW-DISPOSITION.md by hand: CR-01..CR-03 and WR-01..WR-08 `disposition: fixed` in the frontmatter and `fixed` in the table, with Source `fix <short hash> (19.8-05)`; IN-01..IN-09 `deferred` with Source `todo .planning/todos/pending/2026-10-22-console-review-info-findings.md`; `open:` set to the number of findings still open (0). Create the todo in the existing format: frontmatter `created` (ISO time), `title: Console review info findings (IN-01..IN-09)`, `area: framework`, `severity: minor`, `g15office: pending` (the orchestrator mirrors it; no g15office tool here), `files:` the files the nine findings name; body: one section per finding with its file, the problem and the review's suggested fix, noting that IN-08's fix now belongs in bonfire.Sanitize (Task 5) and that IN-07 should keep diary's new use of bonfire.Sanitize in mind. Commit both planning files together: `docs(19.8): review fixes recorded, info findings captured as a todo`.
(5) SUMMARY: plan_head_before and plan_head_after, the commits per finding, the RED replay table, the verification results (Docker run or NOT RUN), the API check result, and a Release impact table (finding, commit, module, exported API added, needs require in plugins yes/no, regenerate in apps yes/no). Expected: API added bonfire.WithExitCode and bonfire.Sanitize, no plugin uses them, the generated main is unchanged, so no require bump and no regenerate; any row that differs says why. Add a suggested g15office comment for the orchestrator ("Plan 19.8-05: 11 review findings fixed with pinning tests; info findings tracked in a todo; release v0.4.2 next").</action>
<verify>
<automated>go vet ./... &amp;&amp; go -C examples/hello test -count=1 ./... &amp;&amp; bash scripts/check-phase19.8.sh go &amp;&amp; bash scripts/check-phase19.8.sh docs &amp;&amp; TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh docker</automated>
<fails_when>vet, the example application's tests, or the gate's go, docs or docker stage fail (a pinning test missing, skipped or red by name, a stale docs identifier, or a Docker test red; with no Docker daemon the docker stage refuses, which is a failure here).</fails_when>
<automated>TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./... &amp;&amp; grep -c 'disposition: fixed' .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md | grep -qx 11 &amp;&amp; grep -c 'disposition: deferred' .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md | grep -qx 9 &amp;&amp; test -f .planning/todos/pending/2026-10-22-console-review-info-findings.md</automated>
<fails_when>The full suite with Docker fails, the disposition record does not show 11 fixed and 9 deferred findings, or the todo file is missing.</fails_when>
</verify>
<acceptance_criteria>
- `grep -cE 'TestPrintAboveOrderedNoLoss|TestDelegateExitCodePassthrough|TestDailyRetriesAfterFailure|TestDevStopWaitsForGracefulShutdown|TestMigrateAppliedLines|TestRenderErrorStripsControl|TestWrongDirectoryMasksSecrets' scripts/check-phase19.8.sh` prints at least 7.
- The SUMMARY's RED replay table has eleven rows, each with result "failed on reverted code".
- `grep -cE '^(severity: minor|area: framework|g15office: pending)$' .planning/todos/pending/2026-10-22-console-review-info-findings.md` prints 3 and `grep -cE '^## IN-0[1-9]' .planning/todos/pending/2026-10-22-console-review-info-findings.md` prints 9.
- The SUMMARY holds the Release impact table and the API check result (no exported declaration removed).
- None of this plan's commits (the hashes the SUMMARY lists) includes `.planning/STATE.md` or `.planning/state.json`: for each listed hash, `git show --name-only --format= <hash>` names neither file. Other sessions' edits and commits to them are left alone.
</acceptance_criteria>
<done>Every review fix is pinned by a gated test that is proven to fail without it, the whole framework suite is green with Docker, the review record says what happened to each finding, and plan 06 has the table it needs to release.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| database, remote services and admin input to the operator's terminal | error text, item names and table cells printed by commands |
| command arguments to stderr and CI logs | the wrong-directory hint repeats arguments |
| summer to the delegated app binary | streams, terminal, exit code and signals |
| long-running processes to the filesystem | daily log files over days of uptime |
| framework to shared core plugins and applications | exported APIs and the generated main |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.8-26 | Tampering | bonfire error block, process view, table (WR-05) | medium | mitigate | bonfire.Sanitize applied to every printed line, item, error and cell before styling; tests with color forced on prove OSC and CSI sequences are gone |
| T-19.8-27 | Information disclosure | wrong-directory hint (WR-08) | medium | mitigate | values of flags named like password, passwd, secret, token or key replaced by *** in the Try line; TestWrongDirectoryMasksSecrets |
| T-19.8-28 | Elevation of privilege | summer delegates skip the production question (CR-03) | high | mitigate | the child gets summer's own streams on a terminal, so bonfire.Interactive and ConfirmProceed see a terminal; TestAppStreams pins the choice |
| T-19.8-29 | Tampering | --json and -q data captured through summer (CR-03) | medium | mitigate | the child's stderr goes to stderr, never into stdout; TestDelegateStreams |
| T-19.8-30 | Repudiation | daily log trail lost after one failed open (WR-04) | medium | mitigate | retry after a minute or on the next date, one warning per date; TestDailyRetriesAfterFailure |
| T-19.8-31 | Denial of service | descriptors leaked by replaced daily writers (WR-06) | low | mitigate | retired writers open, append and close per late record; TestRetiredDailyWriterKeepsNoFile |
| T-19.8-32 | Denial of service | signal handling in live views (WR-01) | medium | mitigate | the view relays SIGINT and SIGTERM only while it runs, restores the terminal, then re-raises so the default action or the command's own handler applies; both paths tested in a helper process |
| T-19.8-33 | Tampering | shared plugin and application contracts | medium | mitigate | additions only (WithExitCode, Sanitize); Task 7's API check finds no removed exported declaration; the generated main is untouched (Task 2 verify) |
| T-19.8-SC | Tampering | dependencies | low | accept | this plan adds no module; tests use the standard library, the existing charm modules and testcontainers |
</threat_model>
<verification>
- Task 1 (tracer): CR-01 red then green, -race, three clean runs.
- Task 2: WR-02 and WR-01 tests, generated main untouched.
- Task 3: bonfire, cmd/summer and internal/dev tests for CR-02, CR-03, WR-03, WR-08; docs checker.
- Task 4: diary tests for WR-04 and WR-06 with -race; docs checker.
- Task 5: WR-05 tests with color forced on; goldens unchanged; docs checker.
- Task 6: WR-07 Docker test plus the existing migration tests.
- Task 7: gate names, RED replay of all eleven commits, `go vet ./...`, `go test ./...` with Docker, examples/hello, gate stages go, docs and docker, API check, disposition and todo.
- Feedback sampling: after every commit `go vet ./...` and `go test -short ./...`.
</verification>
<success_criteria>
- CR-01..CR-03 and WR-01..WR-08 fixed, one commit each, each with a gated pinning test proven red without the fix.
- No exported API removed or changed; two additive identifiers documented in the bonfire README and docs.
- `go vet ./...`, `go test ./...` with Docker, examples/hello and the gate's go, docs and docker stages green.
- 19.8-REVIEW-DISPOSITION.md: 11 fixed, 9 deferred; the IN todo exists with `g15office: pending` for the orchestrator.
- The SUMMARY's Release impact table tells plan 06 whether any plugin or application needs a commit.
</success_criteria>
<output>
Create `.planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md` when done, with plan_head_before and plan_head_after, the commit per finding with its pinning test and RED output, the RED replay table, the verification results (Docker run or NOT RUN), the API check, the Release impact table, the todo path, and the suggested g15office comment for the orchestrator.
</output>

View File

@@ -0,0 +1,255 @@
---
phase: 19.8-console-commands-overhaul
plan: 05
subsystem: console
tags: [review-fixes, bonfire, bubbletea, signals, summer, delegates, dev, diary, lagoon, migrations, gate]
status: complete
gap_closure: true
requires:
- phase: 19.8-01..04
provides: the shipped console (v0.4.0, v0.4.1) and its review 19.8-REVIEW.md
provides:
- "CR-01..CR-03 and WR-01..WR-08 fixed, one commit each with a pinning test proven red without the fix"
- "Additive API: bonfire.WithExitCode, bonfire.Sanitize"
- "scripts/check-phase19.8.sh requires the 19 new pinning tests by name"
- "IN-01..IN-09 captured in .planning/todos/pending/2026-10-22-console-review-info-findings.md"
affects: [19.8-06 release v0.4.2, bonfire live views, summer delegates, summer dev, diary daily files, lagoon migrate]
actuals:
tokens: 21548 # chars/4 over the added and removed lines of this plan's diff (86191 chars)
tasks: 7
commits: 13 # MEASURED: git rev-list --count plan_head_before..plan_head_after
plan_head_before: e8f5b1c89c639e5d1bcaf3096ddf3f05ea58864a
plan_head_after: d311d029c3e86736f18fb7e186ca51073fd510b8
tech-stack:
added: []
patterns:
- "Print above a bubbletea program by sending tea.Println(block)() straight to the program queue under a lock, never as a Cmd"
- "Live view registry: owner console + program, check-and-set claim, deferred unregister/finish/wait under one lock"
- "Signal relay scoped to a live view: Kill, unregister, signal.Stop, re-raise the same signal"
- "Re-executed test binary as a helper process (live view signals, delegate exit codes, dev stop grace)"
- "Exit-code carrier error (WithExitCode) honoured by Execute before the usage check"
key-files:
created:
- modules/bonfire/live_signal_unix_test.go
- modules/diary/retire_internal_test.go
- .planning/todos/pending/2026-10-22-console-review-info-findings.md
modified:
- modules/bonfire/live.go
- modules/bonfire/output.go
- modules/bonfire/widgets.go
- modules/bonfire/helpers.go
- modules/bonfire/errors.go
- modules/bonfire/execute.go
- modules/bonfire/README.md
- modules/diary/daily.go
- modules/diary/diary.go
- modules/diary/console.go
- modules/diary/README.md
- modules/lagoon/migrations.go
- modules/lagoon/commands.go
- modules/lagoon/README.md
- cmd/summer/runtime.go
- internal/dev/watch.go
- docs/console/introduction.md
- docs/console/writing-commands.md
- docs/console/scaffolding.md
- docs/services/logging.md
- docs/setup/upgrading.md
- scripts/check-phase19.8.sh
- .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
key-decisions:
- "CR-01: each PrintAbove block is one tea.Println message sent with Program.Send under liveMu (the direct-queue approach held against bubbletea v2.1.0's event loop); liveModel and printAboveMsg removed"
- "WR-01: signal handling lives inside runLive (relay SIGINT/SIGTERM only while a view runs, Kill, re-raise); generated main and bonfire.Execute's context unchanged"
- "CR-02: explicit additive bonfire.WithExitCode carrier; bonfire does not honour arbitrary ExitCode() errors, so plugin commands returning *exec.ExitError still exit 1"
- "WR-03: dev.ShutdownGrace (internal, 15s) shared by summer dev and summer's delegates"
- "WR-04: retry a failed daily open after one minute (writer clock) or on a new date; warning text kept, once per date"
- "WR-06: retired daily writers open, append and close per late record; diary.Close keeps reopen-on-next-record"
- "WR-05: diary's sanitizer moved to bonfire.Sanitize, diary calls it; IN-08 left to the todo"
- "WR-07: migrate reports pending-before minus pending-after, also on a partial failure"
patterns-established:
- "Each review finding: pinning test red on unchanged code, fix, green, one fix(...) commit with test and docs"
requirements-completed: [D-03, D-04, D-10, D-12, D-18, D-19, D-21, D-25, D-27, D-28, D-29]
duration: 40min
completed: 2026-10-22
---
# Phase 19.8 Plan 05: Code Review Fixes Summary
**The 11 critical and warning findings of the 19.8 code review are fixed. Each fix is its own commit, and its pinning test fails when the fix is reverted. Lines printed above live views keep their order and none is lost. A live view takes lines only from the console that started it. Ctrl+C during a view restores the terminal. summer's delegates keep the app binary's exit code and give it the terminal. summer dev waits for serve's drain. Daily log files retry after a failed open, and replaced daily writers are retired. Error blocks, process views and tables strip control characters. Migrate and PendingMigrations read one list of migration sets. The wrong-directory hint masks secret flag values. The nine info findings are in one todo.**
## Performance
- **Duration:** about 40 min
- **Started:** 2026-10-22T00:04:11Z
- **Completed:** 2026-10-22T00:44:00Z
- **Tasks:** 7 of 7
- **Files modified:** 33 (1695 insertions, 282 deletions)
## Commits per finding
| Finding | Commit | Subject | Pinning tests |
|---------|--------|---------|---------------|
| CR-01 | b077dc3 | fix(bonfire): keep the order of lines printed above a live view and drain them before it ends | TestPrintAboveOrderedNoLoss (spinner, progress, process, concurrent_finish) |
| WR-02 | 86e4e27 | fix(bonfire): reroute only the owning console's lines to a live view | TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters |
| WR-01 | d151a25 | fix(bonfire): restore the terminal when a live view is interrupted | TestLiveViewRestoresTerminalOnSignal (default, own_handler) |
| CR-02 | 323413c | fix(summer): pass the app binary's exit code through and print its error once | TestDelegateExitCodePassthrough, TestWithExitCode |
| CR-03 | 9c89705 | fix(summer): give delegated commands the terminal and keep their stderr apart | TestDelegateStreams, TestAppStreams |
| WR-03 | fc7706d | fix(dev): give serve the full shutdown grace before killing it | TestDevStopWaitsForGracefulShutdown |
| WR-08 | 9b62285 | fix(bonfire): mask secret flag values in the wrong-directory hint | TestWrongDirectoryMasksSecrets |
| WR-04 | c586c6f | fix(diary): retry a daily file that failed to open | TestDailyRetriesAfterFailure |
| WR-06 | 39677f1 | fix(diary): retire replaced daily writers and serialise ApplyGlobals | TestRetiredDailyWriterKeepsNoFile, TestApplyGlobalsRebuildsOnce |
| WR-05 | a9025c0 | fix(bonfire): strip control characters from error blocks, process views and tables | TestSanitize, TestRenderErrorStripsControl, TestProcessViewStripsControl, TestTableStripsControl |
| WR-07 | 020fe83 | fix(lagoon): one migration set list for Migrate and PendingMigrations | TestMigrateAppliedLines (Docker) |
| gate | 1bf64b0 | test(19.8): gate requires the review fixes' tests | (all of the above by name) |
| records | d311d02 | docs(19.8): review fixes recorded, info findings captured as a todo | n/a |
No other session's commit sits between them. None of these commits touches `.planning/STATE.md` or `.planning/state.json`.
## RED output on the code before each fix
Each pinning test was first run on the unchanged code (only the test added) during execution.
- **CR-01** TestPrintAboveOrderedNoLoss: `spinner: run 0: line 15 is line-016, want line-015`; `progress: run 0: line 67 is line-068, want line-067`; `process: run 3: line 67 is line-068, want line-067`; `concurrent_finish: run 0: bg-016 printed 0 times` (lost), with `bg-014` printed before `bg-013`.
- **WR-02** TestPrintlnStaysWithItsConsole: `console B's writer lacks "b-println"` / `console B's "b-println" printed above console A's view`. TestLiveViewSingleOwner: `round 0: 0 consoles printed their plain line to their own writer, want 1` (B's `[...] label-0` landed in A's view). TestLiveViewPanicUnregisters: `the panicked view is still registered`.
- **WR-01** TestLiveViewRestoresTerminalOnSignal/default: `cursor left hidden: no show-cursor after the last hide-cursor in "\x1b[?25l\x1b[?2004h\r\x1b[J"`. The own_handler subtest passed on the old code too. It pins that the fix keeps the command's own handler working.
- **CR-02** TestDelegateExitCodePassthrough/exit_code: `exit 1, want the binary's 2` and `summer printed an error block of its own: stderr "\n ERROR summer: migrate --bogus: exit status 2 ..."`. The test ran on the code after the pure refactor that factored out `runAppCmd`. TestWithExitCode is new API, so its RED is a compile failure.
- **CR-03** TestDelegateStreams: `the binary's stderr landed in summer's stdout: "data-line\nlog-line\n"`.
- **WR-03** TestDevStopWaitsForGracefulShutdown: `child was not allowed to finish its shutdown: signal: killed`, after 2.02s.
- **WR-08** TestWrongDirectoryMasksSecrets: `Try = "cd ..; {bin} admin:create --email a@b.c --password hunter2", want "... --password ***"`, and the same for `--password=`, `--client-secret`, `--api-token=`, `--key` and `--PASSWD`.
- **WR-04** TestDailyRetriesAfterFailure: `one_minute_later: read daily file: ... summer-2026-10-21.log: no such file or directory`; `next_date: ... summer-2026-10-22.log: no such file or directory`; `next_date_fails_too: 1 warnings over two failed dates, want 2`.
- **WR-06** TestRetiredDailyWriterKeepsNoFile: `a late record through the replaced writer left its file open`. TestApplyGlobalsRebuildsOnce: `the channels were rebuilt 2 times, want 1`, then `3 times` (it failed in 2 of 3 runs, because the race is timing-dependent).
- **WR-05** TestRenderErrorStripsControl: `error block carries "\x1b]0;pwned\a"` (with color on, the OSC title and `\x1b[2J` reached the message, explanation, Try and Caused-by lines). TestProcessViewStripsControl: `process view carries "\x1b]0;pwned\a"`. TestTableStripsControl: `terminal true: table carries "\x1b]0;pwned\a"` (the BEL also broke the box width), and the same with `terminal false`.
- **WR-07** TestMigrateAppliedLines (Docker): `the set applied before the failure is not reported` and `framework set summercms.attach / summercms.cabana / summercms.deferred / summercms.conga not reported`.
## RED replay from the commits (Task 7)
For each fix commit C: a detached worktree at C under the session scratchpad, `git show C -- . ':!*_test.go' ':!*.md' | git apply -R`, then C's pinning tests by name, then the worktree removed.
| Finding | Commit | Pinning test | RED kind | Result |
|---------|--------|--------------|----------|--------|
| CR-01 | b077dc3 | TestPrintAboveOrderedNoLoss | compile (`undefined: printLine`: TestLiveModels in the same test file uses the helper the fix adds) | failed on reverted code |
| WR-02 | 86e4e27 | TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters | assertion | failed on reverted code |
| WR-01 | d151a25 | TestLiveViewRestoresTerminalOnSignal | assertion (default: cursor left hidden) | failed on reverted code |
| CR-02 | 323413c | TestDelegateExitCodePassthrough | compile (`undefined: runAppCmd`, factored out in the same commit; TestWithExitCode tests the new exported identifier) | failed on reverted code |
| CR-03 | 9c89705 | TestDelegateStreams, TestAppStreams | compile (`undefined: appStreams`, newAppCmd arity) | failed on reverted code |
| WR-03 | fc7706d | TestDevStopWaitsForGracefulShutdown | compile (`undefined: ShutdownGrace`) | failed on reverted code |
| WR-08 | 9b62285 | TestWrongDirectoryMasksSecrets | assertion | failed on reverted code |
| WR-04 | c586c6f | TestDailyRetriesAfterFailure | assertion | failed on reverted code |
| WR-06 | 39677f1 | TestRetiredDailyWriterKeepsNoFile, TestApplyGlobalsRebuildsOnce | assertion | failed on reverted code |
| WR-05 | a9025c0 | TestSanitize, TestRenderErrorStripsControl, TestProcessViewStripsControl, TestTableStripsControl | compile (`undefined: Sanitize`, the new exported identifier) | failed on reverted code |
| WR-07 | 020fe83 | TestMigrateAppliedLines (Docker) | assertion | failed on reverted code |
The plan expected compile REDs only for the two new exported identifiers (CR-02, WR-05). Four replays (CR-01, CR-02, CR-03, WR-03) compile-fail because the fix commit adds an unexported helper or constant that the test file uses. For all four, the section above records the assertion RED from running the test on the unchanged code before the fix.
## Verification
| Check | Result |
|-------|--------|
| `go vet ./...` | PASS |
| `go test -short ./...` after every commit | PASS. The known flake TestOIDCSubjectIsRandomSmoke (deferred-items.md: a random UUID contains `ada`) failed once after the WR-07 commit and passed on one rerun |
| `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...` (Docker, run directly) | PASS |
| `go -C examples/hello test -count=1 ./...` | PASS |
| `bash scripts/check-phase19.8.sh self-test` / `go` / `hello` / `docs` | PASS / PASS (the race stage over diary, sunscreen, bonfire and docsite included) / PASS / PASS |
| `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh docker` | PASS (TestServeLifecycle; six lagoon tests including TestMigrateAppliedLines; three cabana tests) |
| `go test -count=3 -race` for the CR-01, WR-02 and WR-06 tests | PASS |
| Goldens (`modules/bonfire/testdata`) | unchanged |
| Generated main (`internal/build`) and `cmd/summer/main.go` | untouched by every commit |
Docker-backed tests: **run** (direct Docker socket; `sudo -u golem` was not needed).
## API check
The check took the `func` and `type` lines removed in non-test Go files under `modules/` between plan_head_before and HEAD, kept those with exported names, and removed those added again (`comm -23`). Three lines remain: the `Init`, `Update` and `View` methods of the **unexported** type `liveModel`, which CR-01 deleted. They are not public API. **No exported framework declaration was removed or changed its signature.** Exported identifiers added: `bonfire.WithExitCode(err error, code int) error` and `bonfire.Sanitize(s string) string`. The `Error` and `Unwrap` methods of the unexported type `exitCodeError` are also new. Unexported signatures changed: `runLive` now returns `(started bool, err error)`, and `newAppCmd` takes separate stdout and stderr writers.
## Release impact (for plan 06)
| Finding | Commit | Module | Exported API added | Require bump in plugins | Regenerate in apps |
|---------|--------|--------|--------------------|-------------------------|--------------------|
| CR-01 | b077dc3 | bonfire | none | no | no |
| WR-02 | 86e4e27 | bonfire | none | no | no |
| WR-01 | d151a25 | bonfire | none | no | no |
| CR-02 | 323413c | bonfire, cmd/summer | bonfire.WithExitCode | no (no plugin uses it) | no |
| CR-03 | 9c89705 | cmd/summer | none | no | no |
| WR-03 | fc7706d | internal/dev, cmd/summer | none (dev.ShutdownGrace is internal) | no | no |
| WR-08 | 9b62285 | bonfire | none | no | no |
| WR-04 | c586c6f | diary | none | no | no |
| WR-06 | 39677f1 | diary | none | no | no |
| WR-05 | a9025c0 | bonfire, diary | bonfire.Sanitize | no (no plugin uses it) | no |
| WR-07 | 020fe83 | lagoon | none | no | no |
The generated main is unchanged, so no application needs a regenerate commit. Applications get the fixes by moving to v0.4.2 (`require` bump plus `go mod tidy`) and rebuilding their binary. The fixes reach plugins through the application's framework version, so no shared plugin needs a commit. Developers get the summer CLI fixes (CR-02, CR-03, WR-03) by reinstalling `summer` at v0.4.2. `docs/setup/upgrading.md` describes the delegate changes as "Since 0.4.2".
## Deviations from Plan
### Auto-fixed Issues
**1. [Rule 3 - Blocking] WR-06 tests live in a new internal test file**
- **Found during:** Task 4
- **Issue:** TestRetiredDailyWriterKeepsNoFile and TestApplyGlobalsRebuildsOnce need unexported state (`dailyWriter`, `gens`, `install`), but daily_test.go and diary_test.go are the external package `diary_test`.
- **Fix:** Added `modules/diary/retire_internal_test.go` (package diary). The gate runs it by name like the others.
- **Commit:** 39677f1
**2. [Rule 2 - Missing critical] Sanitize also applied to the spinner's and progress bar's current item**
- **Found during:** Task 5
- **Issue:** `Current(item)` shows the same kind of untrusted item name as the process view, in the spinner and progress frames.
- **Fix:** `Sanitize(m.current)` in both views, alongside the planned call sites.
- **Commit:** a9025c0
**3. [Rule 1 - Bug] Delegate long help no longer says "summer fails when the app binary fails"**
- **Found during:** Task 3 (CR-02)
- **Fix:** The text now says "When the app binary fails, summer exits with its exit code."
- **Commit:** 323413c
**4. TestAppStreams takes the terminal decision as a parameter**
- **Found during:** Task 3 (CR-03)
- **Issue:** From package main, a terminal `bonfire.Output` can only be built on a pseudo-terminal.
- **Fix:** `appStreams(out, terminal bool)`. runAppBinary passes `bonfire.IsTerminal(out)`, and the test checks both branches. TestDelegateStreams runs the whole delegate path with a shell-script binary in a temporary application.
- **Commit:** 9c89705
**5. Migrate's wording when `gdb.DB()` fails**
- **Found during:** Task 6
- **Issue:** Migrate now reads `migrationSets`, which calls `gdb.DB()` before any set runs. That error now reads `lagoon: migration sets: ...` instead of `lagoon: migrate queue: ...`. The wording of each set's migrate error (`system_files`, `backend admin`, `deferred bindings`, `queue`, plugin ID) is kept through a `label` field.
- **Commit:** 020fe83
**6. TestLiveModels "print above" subtest rewritten**
- **Found during:** Task 1
- **Issue:** `liveModel` and `printAboveMsg` were removed. The plan allowed this.
- **Fix:** The subtest now asserts that `printLine` is one message rather than a command. The end-to-end subtest sends `printLine`.
- **Commit:** b077dc3
---
**Total deviations:** 6 (1 blocking, 1 missing critical, 1 bug, 3 recorded choices). **Impact:** none on scope. No exported API changed beyond the two planned additions.
## Issues Encountered
- `gofmt -l` reports pre-existing formatting drift in files this plan did not create (`cmd/summer/runtime.go`'s rows struct, `cmd/summer/main.go`, several cabana, compass, tide and flare files). It is out of scope and left alone. The code this plan added is gofmt-clean.
- The wristband flake from deferred-items.md recurred once (see Verification).
## Known Stubs
None.
## Todo
`.planning/todos/pending/2026-10-22-console-review-info-findings.md`: IN-01..IN-09, `severity: minor`, `area: framework`, `g15office: pending` (the orchestrator mirrors it). It notes that IN-08's fix now belongs in `bonfire.Sanitize` and that IN-07 must keep diary's use of `bonfire.Sanitize` in mind.
## Suggested g15office comment (for the orchestrator)
"Plan 19.8-05: 11 review findings fixed with pinning tests; info findings tracked in a todo; release v0.4.2 next."
## Next Phase Readiness
Plan 19.8-06 can tag v0.4.2 once the user gives the go-ahead. Nothing was pushed or tagged.
## Self-Check: PASSED
All created files exist on disk. All 13 commit hashes listed above are in `git log` (b077dc3, 86e4e27, d151a25, 323413c, 9c89705, fc7706d, 9b62285, c586c6f, 39677f1, a9025c0, 020fe83, 1bf64b0, d311d02).

View File

@@ -0,0 +1,211 @@
---
phase: 19.8-console-commands-overhaul
plan: 06
type: execute
wave: 2
depends_on: ["19.8-05"]
gap_closure: true
files_modified:
- .planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md
- ../sm-demo-app/plugins/demo/content/ROADMAP.md
- ../sm-demo-app/plugins/demo/roadmap/names.go
autonomous: false
requirements: [D-26]
user_setup: []
estimate:
tokens: 60000
raw_tokens: 60000
tasks: 3
confidence: low
must_haves:
truths:
- "D-26: nothing is tagged or pushed before the Task 1 reply, which the SUMMARY records verbatim; before the reply no tag points at HEAD."
- "On 'release vX.Y.Z' (proposed v0.4.2): after git fetch, origin/master..master holds only plan 05's code commits and planning documents (this phase's and other sessions'); vet, the short suite and the docs checker are green at the commit that gets tagged; an annotated vX.Y.Z tag listing the fixes in user words points at that commit; master is pushed as exactly that commit, then the tag; nothing is force-pushed; the same-day changelog post with user-facing notes under ## Fixed exists and its URL is in the SUMMARY; the token is never printed."
- "On 'push-only': the same range check and checks, then master pushed as the tested commit; no tag, no changelog post. On 'hold': nothing leaves this machine."
- "Shared plugins get a require bump and applications a regenerate commit only for rows of plan 05's Release impact table that say so (expected: none, because the two API additions are used by no plugin and the generated main did not change)."
- "sm-demo-app's roadmap snapshot equals origin/master's .planning/ROADMAP.md, every phase in it has a Polish title and every open phase a Polish summary, in separate sm-demo-app commits (Polish names first, then the snapshot) that are pushed only under 'release' or 'push-only'; when the snapshot already matches, sm-demo-app is not touched."
- "`PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all` ends with 'Phase 19.8 gate passed' (TestTerminalCommands may print NOT RUN)."
- "Nothing is deployed in any option."
artifacts:
- path: ".planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md"
provides: "the release proposal, the reply, the tag, pushed ranges, the changelog URL and the --all stage summary"
contains: "## Task 1 reply"
key_links:
- from: "framework tag vX.Y.Z"
to: "https://summercms.io/changelog"
via: "scripts/changelog-post.py source and publish in ../sm-summercmsio-app"
pattern: "changelog-post.py"
- from: "../sm-demo-app/plugins/demo/content/ROADMAP.md"
to: "origin/master:.planning/ROADMAP.md"
via: "scripts/sync-roadmap.sh with ROADMAP_REF=origin/master; the gate's landing stage checks it"
pattern: "sync-roadmap"
prohibitions:
- statement: "No push or tag before the Task 1 reply; never a force-push; never a deploy."
status: unresolved
verification: judgment
- statement: "Another session's code commit in origin/master..master stops the release: nothing is pushed until the user decides."
status: unresolved
verification: judgment
- statement: "The changelog token is never printed, copied or minted; a missing or refused token stops and asks the user."
status: unresolved
verification: judgment
---
## Phase Goal
ROADMAP Goal (not in user-story format; plans 01-04 recorded the decision not to invent one): console commands reach the Artisan bar. This plan (gap closure 2 of 2, wave 2) publishes plan 05's review fixes to the applications' build path: a patch tag with its changelog post, under a go-ahead, then proves the whole phase again with the gate.
## Spec-less probe fallback
Spec-less probe fallback skipped: no REQUIREMENTS.md IDs and no SPEC.md (as in plans 01-05). The release follows D-26 (nothing pushed or tagged without the go-ahead) and the procedure of plan 03 Task 7 step (1) and plan 04 Tasks 5-6.
## Decisions taken in this plan
- **Same shape as v0.4.1.** The checkpoint and the release copy plan 04 Tasks 5-6: range check after a fetch, annotated tag, push of the exact tagged commit to master, then the tag, then the changelog post. The proposed tag is v0.4.2 unless another tag appeared after v0.4.1, in which case the proposal is the next patch after the newest tag.
- **Plugins and applications.** Only rows of plan 05's Release impact table that say "needs require" or "regenerate" lead to commits there; plan 05 expects none. A consumer picks the fixes up with its next framework require bump.
- **Demo roadmap snapshot.** The gate's landing stage compares sm-demo-app's `plugins/demo/content/ROADMAP.md` with origin/master's `.planning/ROADMAP.md`. The release push publishes this phase's and other sessions' roadmap changes, which turned the landing stage red after v0.4.1. Task 3 re-syncs it when the check fails, adding Polish names first for any new phase (TestRoadmapPolishTitles), so every sm-demo-app commit is green.
<objective>
Release plan 05's fixes as v0.4.2 under the user's go-ahead, with the same-day changelog post, keep the demo roadmap snapshot in step with what was published, and finish with a green phase gate.
Purpose: D-26 (nothing published without the go-ahead) and the changelog rule in CLAUDE.md; the fixes only help applications once they are tagged.
Output: on release, tag vX.Y.Z on origin and a changelog post; on push-only, master on origin; in every option, a green `--all` run in the SUMMARY.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@.planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md
@.planning/phases/19.8-console-commands-overhaul/19.8-04-SUMMARY.md
Release procedure to repeat: 19.8-03-PLAN.md Task 7 step (1) and 19.8-04-PLAN.md Tasks 5 and 6 (and the "Task 6: published" section of 19.8-04-SUMMARY.md). Changelog: ../sm-summercmsio-app/DEPLOY.md "Posting to the changelog" and the usage header of ../sm-summercmsio-app/scripts/changelog-post.py.
Concurrency: other sessions commit planning documents (and sometimes code) to summercms.go and may push. Stage by explicit path, never `git add -A`, never touch `.planning/STATE.md` or `.planning/state.json` by hand, no co-author tags. In sm-demo-app commit only the two roadmap paths and check `git status --porcelain` and `git log origin/master..master` there first. Docker tests need `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1`; if the socket is denied, run through `sudo -u golem`.
</context>
<tasks>
<task type="checkpoint:decision" gate="blocking">
<name>Task 1: Go-ahead to publish the review fixes: release v0.4.2, push-only, or hold</name>
<read_first>19.8-05-SUMMARY.md (fix commits, RED replay, verification, Release impact table), 19.8-04-SUMMARY.md ("Task 6: published": how v0.4.1 went out), 19.8-04-PLAN.md Task 5 (the checkpoint this one copies), CLAUDE.md (Commits and concurrency, the changelog rule)</read_first>
<acceptance_criteria>
- Before the checkpoint returns, a draft 19.8-06-SUMMARY.md holds a `## Release proposal` section: the output of `git fetch origin` and of `git log --format='%h %s' origin/master..master`, each commit classified as plan 05 code, planning documents (this phase or another session) or other code (with its paths from `git show --name-only --format= <hash>`); the newest tag (`git tag --sort=-v:refname`, first line) and the proposed tag; plan 05's Release impact table; the result of `ROADMAP_REF=origin/master ../sm-demo-app/scripts/sync-roadmap.sh --check`; and plan 05's verification status (Docker run or NOT RUN).
- Before the reply, `T=$(git tag --points-at HEAD) && test -z "$T"` exits 0.
- After the reply, the SUMMARY records it verbatim under `## Task 1 reply`, with the chosen tag on a release.
</acceptance_criteria>
<decision>Publish plan 05's fixes now, and with which patch tag?</decision>
<context>Plan 05 fixed the eleven critical and warning findings of the code review, each in its own commit with a gated pinning test, and recorded a Release impact table. Releasing means an annotated patch tag (proposed v0.4.2) on the tested master commit, pushing master as exactly that commit and then the tag, and a same-day changelog post with the fixes under ## Fixed. Pushing master also publishes every unpushed planning-document commit of other sessions listed in the proposal; another session's code commit in the range blocks the release until you decide. Plugins and applications get commits only where the Release impact table says so (expected: none). After the push, sm-demo-app's roadmap snapshot is re-synced if it no longer matches origin/master (Task 3). Nothing is deployed in any option. If the proposal shows another session's code in the range, a red check or a Docker NOT RUN in plan 05, the recommended answer is hold.</context>
<options>
<option id="release">
<name>release &lt;tag&gt; (for example "release v0.4.2")</name>
<pros>Applications and plugins can pin the fixes; the changelog shows them the same day; the demo roadmap snapshot follows the published roadmap.</pros>
<cons>Another tag and changelog post one day after v0.4.1; publishes other sessions' planning commits.</cons>
</option>
<option id="push-only">
<name>push-only</name>
<pros>The fixes are on origin/master for the next release; no tag or post today.</pros>
<cons>Tagged consumers keep v0.4.1 and its defects until the next tag.</cons>
</option>
<option id="hold">
<name>hold</name>
<pros>Nothing leaves this machine.</pros>
<cons>The fixes stay local; verification sees them unpublished.</cons>
</option>
</options>
<resume-signal>Reply "release vX.Y.Z", "push-only" or "hold". "release" and "push-only" also authorise pushing sm-demo-app's Polish-name and roadmap-snapshot commits from Task 3 when they are its only unpushed commits. A release also authorises the plugin require bumps and application regenerate commits that plan 05's Release impact table names (expected none). Nothing is ever force-pushed or deployed.</resume-signal>
</task>
<task type="auto">
<name>Task 2: Under the Task 1 reply, publish: range check, checks, annotated tag on the tested commit, push master then the tag, same-day changelog post; plugin and application commits only where plan 05's table asks</name>
<precondition>19.8-06-SUMMARY.md (draft) records the Task 1 reply under `## Task 1 reply`. On "hold" this task records "release held, nothing published" and stops.</precondition>
<files>.planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md</files>
<read_first>19.8-06-SUMMARY.md draft (Release proposal, Task 1 reply), 19.8-05-SUMMARY.md (fix commits and Release impact table), 19.8-03-PLAN.md Task 7 step (1), 19.8-04-SUMMARY.md ("Task 6: published"), ../sm-summercmsio-app/DEPLOY.md ("Posting to the changelog"), ../sm-summercmsio-app/scripts/changelog-post.py (usage header only)</read_first>
<action>Per D-26, under the recorded reply. Never force-push, never deploy, never print the changelog token.
On "release vX.Y.Z":
(1) `git fetch origin`; list `git log --format='%h %s' origin/master..master` and classify each commit with `git show --name-only --format= <hash>`: a commit touching only `.planning/` is a planning document and may go out; a commit touching anything else must be one of plan 05's commits listed in its SUMMARY. Any other code commit stops the task: record it in the SUMMARY and ask the user (nothing pushed). Confirm the newest tag is still the one the proposal named; if another tag appeared since, stop and ask (the reply's tag may now be wrong).
(2) At master HEAD with a clean tree for this phase's paths (`git status --porcelain`; other sessions' STATE.md and state.json edits are left alone): `go vet ./...`, `go test -count=1 -short ./...` and `go run ./cmd/summer docs:build --check` green. Record the tested commit's hash.
(3) `git tag -a vX.Y.Z <tested hash>` with a message: first line `SummerCMS vX.Y.Z`, a blank line, one sentence ("Fixes from the review of the 0.4 console."), then one bullet per fix in user words (no CR/WR IDs, no planning or quick codes): lines printed during spinners and progress bars keep their order and none is lost; Ctrl+C during a spinner or progress bar restores the cursor; a command's output never lands in another command's live view; summer's delegated commands get the terminal (production question, styled output), keep their stderr apart from data and exit with the binary's exit code; summer dev lets serve finish its shutdown; a daily log file that failed to open is retried; replaced log files are closed; error blocks, process views and tables strip terminal control characters; the wrong-directory hint masks password, secret, token and key values; migrate reports what it actually applied (adjust to what plan 05 actually shipped).
(4) `git push origin <tested hash>:refs/heads/master` (a fast-forward of exactly the tagged commit, so commits landing meanwhile are not published), then `git push origin vX.Y.Z`; `git ls-remote --exit-code --tags origin vX.Y.Z` exits 0.
(5) Changelog, the same day, from ../sm-summercmsio-app: `scripts/changelog-post.py source vX.Y.Z`; write user-facing Markdown notes to a file in the session scratchpad: no H1, a `## Fixed` section with the bullets above (more readable sentences are fine; no planning IDs, no quick codes); then `scripts/changelog-post.py publish vX.Y.Z <notes file> --excerpt "<one line: console fixes for 0.4>"`. If the token file is missing or the token is refused, stop and ask the user. Record the post URL.
(6) For each plan 05 Release impact row marked "needs require": in that plugin repository `go get git.golem15.com/golem15/summercms@vX.Y.Z`, `go mod tidy`, vet and short tests, one commit `chore: require SummerCMS vX.Y.Z`, push. For each row marked "regenerate": regenerate main.go, build, `bin/<app> list`, one commit `build: regenerate main for SummerCMS vX.Y.Z`, push. With no such rows record "plugins and applications: nothing to publish".
On "push-only": steps (1) and (2), then `git push origin <tested hash>:refs/heads/master`; no tag, no changelog post.
Record in the SUMMARY: the range classification after the fetch, the tested hash, the tag object, the pushed range, the changelog URL, and every plugin or application commit (or "none").</action>
<verify>
<automated>T="${RELEASE_TAG:?set RELEASE_TAG to the tag named in the Task 1 reply}" &amp;&amp; git ls-remote --exit-code --tags origin "$T" &amp;&amp; git fetch origin &amp;&amp; git merge-base --is-ancestor "$T^{commit}" origin/master &amp;&amp; test "$(git cat-file -t "$T")" = tag</automated>
<fails_when>On release: the newest reachable tag is not on origin, the tagged commit is not on origin/master, or the tag is lightweight instead of annotated. On "push-only" or "hold" this command is not run; the SUMMARY records the pushed range or "nothing published".</fails_when>
</verify>
<acceptance_criteria>
- On release: `git ls-remote --exit-code --tags origin vX.Y.Z` exits 0, `git cat-file -t vX.Y.Z` prints `tag`, and the SUMMARY holds the changelog post URL (https://summercms.io/changelog/...).
- The SUMMARY lists every published repository with its pushed range, or "nothing published" on hold.
- The SUMMARY's range classification lists no "other code" commit, or records that the task stopped because of one.
</acceptance_criteria>
<done>The fixes are published exactly as the user chose, with a tag and changelog post on release, and nothing else of another session's code went out.</done>
</task>
<task type="auto">
<name>Task 3: The demo roadmap snapshot matches the published roadmap (Polish names for new phases first, then the snapshot, in sm-demo-app, only when the check fails), and the whole phase gate is green with Docker required</name>
<files>../sm-demo-app/plugins/demo/roadmap/names.go, ../sm-demo-app/plugins/demo/content/ROADMAP.md, .planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md</files>
<read_first>../sm-demo-app/scripts/sync-roadmap.sh (header: ROADMAP_REF, --check), ../sm-demo-app/plugins/demo/roadmap/names.go (phaseText entries, the client-name denylist), ../sm-demo-app/plugins/demo/roadmap/*_test.go (TestRoadmapPolishTitles, TestRoadmapNoClientNames), `git -C ../sm-demo-app log --oneline -5` (the earlier "Polish name for roadmap phase" and "sync the ROADMAP.md snapshot" commits as the pattern), scripts/check-phase19.8.sh (run_landing), 19.8-04-SUMMARY.md ("Gate after the release": the red landing stage after v0.4.1)</read_first>
<action>(1) Conditional roadmap sync. `git fetch origin` in summercms.go, then `ROADMAP_REF=origin/master ../sm-demo-app/scripts/sync-roadmap.sh --check`. If it prints ok, record "snapshot in step, sm-demo-app untouched" and go to (2). Otherwise, in sm-demo-app: check `git status --porcelain` and `git log --oneline origin/master..master` (another session's uncommitted roadmap edits or unpushed commits there: stop and report); run `ROADMAP_REF=origin/master scripts/sync-roadmap.sh` to write the snapshot, then `go test -count=1 -short -run '^(TestRoadmapParse|TestRoadmapDeckOrder|TestRoadmapNoClientNames|TestRoadmapPolishTitles)$' ./plugins/demo/roadmap`. If TestRoadmapPolishTitles names a phase without a Polish title (or an open phase without a Polish summary), add its `phaseText` entry to plugins/demo/roadmap/names.go in the style of the existing entries: a short Polish title and, for open phases, a one-line Polish summary of the roadmap goal, naming no client application (TestRoadmapNoClientNames enforces the denylist). Commit so that every commit is green: first names.go alone (`feat(demo): Polish name for roadmap phase <N>`, one commit naming all new phases if several), then the snapshot alone (`chore: sync the ROADMAP.md snapshot (summercms <short hash of origin/master>)`), staged by path. Re-run the four roadmap tests. Push sm-demo-app only when the Task 1 reply was "release" or "push-only" and `git log origin/master..master` there lists only these commits; on "hold" leave them local and say so. Never deploy.
(2) Gate. Run `PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all` (through `sudo -u golem` if the Docker socket is denied) and paste its stage summary into the SUMMARY. A red stage is diagnosed and fixed through the owning repository's normal flow (a framework defect goes back to a plan 05 style fix commit and blocks completion; a sibling repository's unrelated pre-existing failure is recorded with evidence that it also fails without this plan's commits) and the gate re-run until green.
(3) Record in the SUMMARY a suggested g15office comment for the orchestrator (no g15office tool here): on release "Phase 19.8 review fixes released as SummerCMS vX.Y.Z (changelog: <URL>); phase gate green; ready for /msd-verify-work", otherwise the matching hold or push-only wording.</action>
<verify>
<automated>ROADMAP_REF=origin/master ../sm-demo-app/scripts/sync-roadmap.sh --check &amp;&amp; LOG="${TMPDIR:?set TMPDIR to the session scratchpad}/check-phase19.8-06.log" &amp;&amp; PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all &gt;"$LOG" 2&gt;&amp;1 &amp;&amp; tail -20 "$LOG" &amp;&amp; grep -q '^Phase 19.8 gate passed$' "$LOG" &amp;&amp; ! grep -qE '^(FAIL|NOT RUN docker)' "$LOG"</automated>
<fails_when>The demo snapshot differs from origin/master's ROADMAP.md, the gate exits non-zero or lacks "Phase 19.8 gate passed", a stage line starts with FAIL, or docker did not run (PHASE198_REQUIRE_DOCKER=1 turns a missing daemon into a failure).</fails_when>
</verify>
<acceptance_criteria>
- The SUMMARY holds the --all stage summary with every stage PASS (TestTerminalCommands may be NOT RUN).
- When sm-demo-app was changed: `git -C ../sm-demo-app log --format=%s -2` shows the Polish-name commit (when one was needed) before the snapshot commit, `git -C ../sm-demo-app show --name-only --format= HEAD` names only plugins/demo/content/ROADMAP.md, and on release or push-only `git -C ../sm-demo-app log --oneline origin/master..master` prints nothing.
- When sm-demo-app was not changed, the SUMMARY says the snapshot was already in step.
</acceptance_criteria>
<done>The published roadmap and the demo deck agree, and one green gate run proves the whole phase after the release.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| this machine to git.golem15.com | pushes and tags publish framework code, other sessions' planning commits and sm-demo-app commits |
| this machine to summercms.io | the changelog post, authenticated by a token file |
| roadmap to the public demo deck | phase titles and summaries shown to prospective clients |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-19.8-34 | Repudiation | release publication | high | mitigate | blocking checkpoint before any push; after a fetch every commit in origin/master..master is classified by its paths, and any code commit not listed by plan 05 stops the release |
| T-19.8-35 | Tampering | pushed master | medium | mitigate | push exactly the tested, tagged commit as a fast-forward (`<hash>:refs/heads/master`); never force-push; vet, short tests and the docs checker green at that commit |
| T-19.8-36 | Information disclosure | changelog token | medium | mitigate | changelog-post.py reads the token file itself; the token is never printed, copied or minted; a missing or refused token stops and asks the user |
| T-19.8-37 | Information disclosure | demo roadmap deck | medium | mitigate | new Polish names follow the existing entries and TestRoadmapNoClientNames rejects client application names before the snapshot commit |
| T-19.8-SC | Tampering | dependencies | low | accept | no module is added; plugin requires move only to this framework's own tag, and only for rows of plan 05's table |
</threat_model>
<verification>
- Task 1: proposal written before the checkpoint; no tag on HEAD before the reply; reply recorded verbatim.
- Task 2: on release, tag annotated and on origin, its commit on origin/master, changelog URL recorded; on push-only, pushed range recorded; on hold, nothing published.
- Task 3: demo snapshot equals origin/master's roadmap; --all green with Docker required.
</verification>
<success_criteria>
- The user's choice is executed exactly: release (tag, push, changelog), push-only or hold; nothing deployed or force-pushed.
- No other session's code went out with the release.
- The demo roadmap snapshot and Polish names are current, in separate sm-demo-app commits, only when needed.
- `bash scripts/check-phase19.8.sh --all` is green with Docker required.
</success_criteria>
<output>
Create `.planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md` when done, with the release proposal, the Task 1 reply verbatim, the range classification, the tested hash, the tag and tag object, the pushed ranges, the changelog post URL, plugin and application commits (or "none"), the sm-demo-app commits (or "snapshot already in step"), the --all stage summary, and the suggested g15office comment for the orchestrator.
</output>

View File

@@ -0,0 +1,92 @@
---
phase: 19.8-console-commands-overhaul
plan: 06
subsystem: release
tags: [release, v0.4.2, changelog, demo-roadmap, gate]
status: draft
gap_closure: true
---
# Phase 19.8 Plan 06: Release of the Review Fixes Summary (draft)
**Draft. Task 1 (the release go-ahead) is waiting for the user's reply. Nothing has been pushed or tagged.**
## Release proposal
Prepared on 2026-10-22 at master `8020b17` (before this draft's own commit). All commands below are read-only towards origin, apart from `git fetch`, which only updates local refs.
### `git fetch origin`
Exit 0, no output: origin/master is still `6c2164a` (`docs(19.8-04): v0.4.1 released; roadmap progress 4/4 plans`). No other session has pushed since v0.4.1.
### `git log --format='%h %s' origin/master..master` (18 commits), classified
| Commit | Subject | Class | Paths |
|--------|---------|-------|-------|
| 8020b17 | docs(19.8-05): roadmap progress 5/6 plans | planning, this phase | .planning/ROADMAP.md |
| 89c8325 | docs(19.8-05): complete review fixes plan | planning, this phase | .planning/phases/19.8-.../19.8-05-SUMMARY.md |
| d311d02 | docs(19.8): review fixes recorded, info findings captured as a todo | planning, this phase | 19.8-REVIEW-DISPOSITION.md, .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| 1bf64b0 | test(19.8): gate requires the review fixes' tests | plan 05 code | scripts/check-phase19.8.sh |
| 020fe83 | fix(lagoon): one migration set list for Migrate and PendingMigrations | plan 05 code (WR-07) | modules/lagoon/{README.md,commands.go,migrations.go,pending_test.go} |
| a9025c0 | fix(bonfire): strip control characters from error blocks, process views and tables | plan 05 code (WR-05) | docs/console/writing-commands.md, modules/bonfire/{README.md,errors.go,errors_test.go,helpers.go,live.go,live_test.go,widgets.go,widgets_test.go}, modules/diary/console.go |
| 39677f1 | fix(diary): retire replaced daily writers and serialise ApplyGlobals | plan 05 code (WR-06) | modules/diary/{daily.go,diary.go,retire_internal_test.go} |
| c586c6f | fix(diary): retry a daily file that failed to open | plan 05 code (WR-04) | docs/services/logging.md, modules/diary/{README.md,daily.go,daily_test.go} |
| 9b62285 | fix(bonfire): mask secret flag values in the wrong-directory hint | plan 05 code (WR-08) | docs/console/writing-commands.md, modules/bonfire/{README.md,errors.go,errors_test.go} |
| fc7706d | fix(dev): give serve the full shutdown grace before killing it | plan 05 code (WR-03) | cmd/summer/runtime.go, docs/console/scaffolding.md, internal/dev/{watch.go,watch_test.go} |
| 9c89705 | fix(summer): give delegated commands the terminal and keep their stderr apart | plan 05 code (CR-03) | cmd/summer/{runtime.go,runtime_test.go}, docs/console/introduction.md, docs/setup/upgrading.md |
| 323413c | fix(summer): pass the app binary's exit code through and print its error once | plan 05 code (CR-02) | cmd/summer/{runtime.go,runtime_test.go}, docs/console/introduction.md, docs/setup/upgrading.md, modules/bonfire/{README.md,errors.go,errors_test.go,execute.go} |
| d151a25 | fix(bonfire): restore the terminal when a live view is interrupted | plan 05 code (WR-01) | modules/bonfire/{README.md,live.go,live_signal_unix_test.go} |
| 86e4e27 | fix(bonfire): reroute only the owning console's lines to a live view | plan 05 code (WR-02) | modules/bonfire/{README.md,helpers.go,live.go,live_test.go,output.go,widgets.go} |
| b077dc3 | fix(bonfire): keep the order of lines printed above a live view and drain them before it ends | plan 05 code (CR-01) | modules/bonfire/{live.go,live_test.go,widgets.go} |
| e8f5b1c | docs(19.8): gap-closure plans for the code review findings and v0.4.2 | planning, this phase | .planning/ROADMAP.md, 19.8-05-PLAN.md, 19.8-06-PLAN.md |
| dd12382 | docs(19.8): code review of the console overhaul | planning, this phase | 19.8-REVIEW.md |
| 0b1d7e5 | docs(19.8): record code review disposition | planning, this phase | 19.8-REVIEW-DISPOSITION.md |
**Result:** 12 plan 05 code commits (all twelve listed in 19.8-05-SUMMARY.md), 6 planning documents of this phase, **0 planning documents of other sessions, 0 other code**. This draft's own commit (`docs(19.8-06): ...`, planning only) will be the 19th. The range check is repeated after a fresh fetch in Task 2 step (1).
### Tags
- Newest tag (`git tag --sort=-v:refname | head -1`): **v0.4.1** (on `8bacc22`, an ancestor of origin/master).
- **Proposed tag: v0.4.2.**
- `T=$(git tag --points-at HEAD) && test -z "$T"`: exits 0 (no tag on HEAD).
### Release impact table (from 19.8-05-SUMMARY.md)
| Finding | Commit | Module | Exported API added | Require bump in plugins | Regenerate in apps |
|---------|--------|--------|--------------------|-------------------------|--------------------|
| CR-01 | b077dc3 | bonfire | none | no | no |
| WR-02 | 86e4e27 | bonfire | none | no | no |
| WR-01 | d151a25 | bonfire | none | no | no |
| CR-02 | 323413c | bonfire, cmd/summer | bonfire.WithExitCode | no (no plugin uses it) | no |
| CR-03 | 9c89705 | cmd/summer | none | no | no |
| WR-03 | fc7706d | internal/dev, cmd/summer | none (dev.ShutdownGrace is internal) | no | no |
| WR-08 | 9b62285 | bonfire | none | no | no |
| WR-04 | c586c6f | diary | none | no | no |
| WR-06 | 39677f1 | diary | none | no | no |
| WR-05 | a9025c0 | bonfire, diary | bonfire.Sanitize | no (no plugin uses it) | no |
| WR-07 | 020fe83 | lagoon | none | no | no |
No row asks for a require bump or a regenerate: **plugins and applications get no commits** under any reply.
### Demo roadmap snapshot
- `ROADMAP_REF=origin/master ../sm-demo-app/scripts/sync-roadmap.sh --check`: **`sync-roadmap: ok`** (exit 0). sm-demo-app is clean (`git status --porcelain` empty) and has no unpushed commits (`origin/master..master` empty).
- Against local master the check fails, as expected: the unpushed `.planning/ROADMAP.md` changes are 19.8's own plan list and progress (`4/4` to `5/6`, plans 05 and 06 listed). They add no new phase, so no Polish name is needed. After a push (release or push-only), Task 3 makes one sm-demo-app commit, `chore: sync the ROADMAP.md snapshot (summercms <hash>)`, and pushes it only under release or push-only.
### Plan 05 verification status and checks at HEAD now
- Plan 05: Docker-backed tests **run** (direct socket): `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...` PASS and the gate's docker stage PASS (TestMigrateAppliedLines included). Gate stages self-test, go, hello, docs PASS.
- Re-run now at `8020b17`: `go vet ./...` **PASS**; `go run ./cmd/summer docs:build --check` **PASS** ("no problems found"); `go test -count=1 -short ./...` **PASS** with `GOCACHE` in the session scratchpad (tmpfs).
- **Environment warning:** the first short run failed in `cmd/summer` (TestNewCommandViaCLI: `go mod tidy ... no space left on device`). `/home` is full (477G, 1.7G free; `~/.cache/go-build` is 33G) and `/media/nvme` is at 99% (19G free). The code is not at fault: the same suite is green with the build cache on tmpfs. Task 2 step (2) and the Task 3 `--all` gate (nuxt generate, Docker images, app builds) will run with `GOCACHE` on tmpfs; freeing space on `/home` (for example `go clean -cache`) is the user's call and was not done here.
### Recommendation
**release v0.4.2.** The range holds only plan 05's code and this phase's planning documents, every check is green (Docker included in plan 05), and the table needs no plugin or application commits.
## Task 1 reply
The user's reply, verbatim:
> release
Read as the recommended option with the proposed tag: **release v0.4.2** (RELEASE_TAG=v0.4.2). The reply authorises an annotated tag v0.4.2 on the tested commit, a fast-forward push of exactly that commit to origin master followed by the tag, the same-day changelog post, the sm-demo-app roadmap snapshot commit and its push (when it is the only unpushed commit there), then the `--all` gate. No force push, no deploy.

View File

@@ -0,0 +1,118 @@
---
phase: 19.8
review: 19.8-REVIEW.md
titles: json
findings:
- id: CR-01
severity: critical
disposition: fixed
title: "Lines printed above a live view are reordered and can be lost"
- id: CR-02
severity: critical
disposition: fixed
title: "summer delegates print a second error block and turn exit code 2 into 1"
- id: CR-03
severity: critical
disposition: fixed
title: "summer delegates hide the terminal from the app and merge its stderr into stdout"
- id: WR-01
severity: warning
disposition: fixed
title: "Ctrl+C during a spinner, progress bar or process view leaves the cursor hidden"
- id: WR-02
severity: warning
disposition: fixed
title: "Any console's Println and Printf are sent to whichever live view is running"
- id: WR-03
severity: warning
disposition: fixed
title: "summer dev kills serve 2 seconds after SIGTERM, but serve drains for up to 10"
- id: WR-04
severity: warning
disposition: fixed
title: "One failed open switches a daily log file off until the process exits"
- id: WR-05
severity: warning
disposition: fixed
title: "Error blocks, process views and tables write untrusted text to the terminal without stripping escape sequences"
- id: WR-06
severity: warning
disposition: fixed
title: "Setup and ApplyGlobals close daily files that other goroutines may still be writing through"
- id: WR-07
severity: warning
disposition: fixed
title: "PendingMigrations keeps its own copy of Migrate's migration sets"
- id: WR-08
severity: warning
disposition: fixed
title: "The wrong-directory hint repeats every argument, including a --password value, on stderr"
- id: IN-01
severity: info
disposition: deferred
title: "A forced-color console drops HideOnTTY records even when stderr is not a terminal"
- id: IN-02
severity: info
disposition: deferred
title: "PeekGlobals and cobra disagree on some argument forms"
- id: IN-03
severity: info
disposition: deferred
title: "isUsage recognises cobra's unknown-command error by its text"
- id: IN-04
severity: info
disposition: deferred
title: "The --json flag is read two different ways"
- id: IN-05
severity: info
disposition: deferred
title: "summer's scaffolding result lines are now hidden by -q"
- id: IN-06
severity: info
disposition: deferred
title: "fang replaces the root help function that NewRootIO installs"
- id: IN-07
severity: info
disposition: deferred
title: "diary pulls the whole TUI stack into security modules"
- id: IN-08
severity: info
disposition: deferred
title: "The console sanitizer passes raw C1 bytes through on its fast path"
- id: IN-09
severity: info
disposition: deferred
title: "The phase gate names consuming applications"
open: 0
total: 20
recorded: 2026-10-21T22:35:50.040Z
---
# Phase 19.8: Code Review Disposition
| Finding | Severity | Disposition | Source |
|---------|----------|-------------|--------|
| CR-01 | critical | fixed | fix b077dc3 (19.8-05) |
| CR-02 | critical | fixed | fix 323413c (19.8-05) |
| CR-03 | critical | fixed | fix 9c89705 (19.8-05) |
| WR-01 | warning | fixed | fix d151a25 (19.8-05) |
| WR-02 | warning | fixed | fix 86e4e27 (19.8-05) |
| WR-03 | warning | fixed | fix fc7706d (19.8-05) |
| WR-04 | warning | fixed | fix c586c6f (19.8-05) |
| WR-05 | warning | fixed | fix a9025c0 (19.8-05) |
| WR-06 | warning | fixed | fix 39677f1 (19.8-05) |
| WR-07 | warning | fixed | fix 020fe83 (19.8-05) |
| WR-08 | warning | fixed | fix 9b62285 (19.8-05) |
| IN-01 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-02 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-03 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-04 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-05 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-06 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-07 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-08 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-09 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
Dispositions: `open` (recorded, not yet triaged), `fixed`, `skipped`, `deferred`.
Set `deferred` by hand and put the reason in the Source cell; both are preserved. A `|` in the reason is kept as prose and escaped on the next run.
Re-running the gate keeps every row it can. A row the current review no longer reports is kept and its Source cell flagged, so a finding does not leave this record silently. ONE exception: when a finding id is REUSED by a different finding, the earlier decision cannot keep a row — the id is taken — and it is dropped. A RECORDED decision (anything but `open`) is named on the console when that happens; a row still at `open` is replaced silently, because `open` records no decision to lose.

View File

@@ -0,0 +1,336 @@
---
phase: 19.8-console-commands-overhaul
reviewed: 2026-10-21T22:33:36Z
depth: standard
diff_range: v0.3.1..v0.4.1 (19.5 commits and the cooler test commit excluded)
files_reviewed: 93
files_reviewed_list:
- cmd/summer/docs.go
- cmd/summer/main.go
- cmd/summer/parity.go
- cmd/summer/runtime.go
- docs/examples/blog/console/publish.go
- examples/hello/main.go
- examples/hello/plugins/greeter/plugin.go
- internal/build/artifact.go
- internal/build/build.go
- internal/build/manifest.go
- internal/build/new.go
- internal/build/scaffold.go
- internal/dev/watch.go
- internal/docsite/check_commands.go
- modules/beachcomber/beachcomber.go
- modules/bonfire/command.go
- modules/bonfire/docs.go
- modules/bonfire/errors.go
- modules/bonfire/execute.go
- modules/bonfire/globals.go
- modules/bonfire/helpers.go
- modules/bonfire/help.go
- modules/bonfire/list.go
- modules/bonfire/live.go
- modules/bonfire/output.go
- modules/bonfire/prompts.go
- modules/bonfire/root.go
- modules/bonfire/selector.go
- modules/bonfire/styles.go
- modules/bonfire/version.go
- modules/bonfire/widgets.go
- modules/bouncer/turnstile.go
- modules/cabana/account.go
- modules/cabana/account_preferences.go
- modules/cabana/account_tokens.go
- modules/cabana/actions.go
- modules/cabana/api_tokens.go
- modules/cabana/auth.go
- modules/cabana/commands.go
- modules/cabana/contracts.go
- modules/cabana/crud.go
- modules/cabana/field_dependent.go
- modules/cabana/field_file.go
- modules/cabana/field_type.go
- modules/cabana/http.go
- modules/cabana/log.go
- modules/cabana/partial_render.go
- modules/cabana/query.go
- modules/cabana/relation_field.go
- modules/cabana/reorder.go
- modules/cabana/singleton.go
- modules/cabana/token_commands.go
- modules/cabana/token_guard.go
- modules/conga/client.go
- modules/conga/commands.go
- modules/conga/conga.go
- modules/conga/worker.go
- modules/cooler/cooler.go
- modules/cooler/memory.go
- modules/cooler/ratelimit.go
- modules/cooler/rediscache.go
- modules/cooler/sitecache.go
- modules/diary/channels.go
- modules/diary/component.go
- modules/diary/config.go
- modules/diary/console.go
- modules/diary/daily.go
- modules/diary/diary.go
- modules/flare/commands.go
- modules/flare/flare.go
- modules/lagoon/attach/bucket.go
- modules/lagoon/attach/thumb.go
- modules/lagoon/commands.go
- modules/lagoon/connection.go
- modules/lagoon/encrypted.go
- modules/lagoon/fill.go
- modules/lagoon/keygen.go
- modules/lagoon/migrations.go
- modules/lagoon/purge.go
- modules/lagoon/transaction.go
- modules/lighthouse/centrifugo/client.go
- modules/lighthouse/centrifugo/commands.go
- modules/lighthouse/lighthouse.go
- modules/phrasebook/translator.go
- modules/postcard/drivers.go
- modules/postcard/mailer.go
- modules/sunscreen/sunscreen.go
- modules/surf/accesslog.go
- modules/surf/routelist_command.go
- modules/surf/serve.go
- modules/surf/summary.go
- modules/wristband/oidc_token.go
- scripts/check-phase19.8.sh
findings:
critical: 3
warning: 8
info: 9
total: 20
status: issues_found
---
# Phase 19.8: Code Review Report
**Reviewed:** 2026-10-21T22:33:36Z
**Depth:** standard
**Files Reviewed:** 93
**Status:** issues_found
## Summary
I reviewed the phase's changed hunks in the 93 non-test source files (`git diff v0.3.1..v0.4.1`). I read the new modules (diary, and bonfire's globals, errors, execute, live, prompts, selector and list) in full. I checked the bubbletea v2.1.0, fang v2.0.1 and gorm v1.31.2 sources wherever bonfire, diary or lagoon depend on how they behave. I confirmed one finding at runtime: a scratch program under a pseudo-terminal (`script -qec`) shows that lines printed during a live view come out in the wrong order and are sometimes lost. Items already listed in `deferred-items.md` are not reported again.
The request log is clean. It logs the route pattern and never the path, query, headers or remote address. The GORM bridge logs SQL with placeholders. The startup summary shows the database only as host, port and name. The redaction wrapping sits on every diary leaf.
The main defects are in three places:
- **Printing above live views.** Lines printed while a spinner, progress bar or process view runs are reordered and sometimes dropped. Any console's output can be redirected to the terminal that owns the view.
- **summer delegates.** Every `summer <app command>` merges the app's stderr into stdout and never gives the app a terminal. The production confirmation is skipped, `--json` output gets log lines mixed in, two error blocks are printed, and exit code 2 becomes 1.
- **Process lifecycle.** Ctrl+C during a spinner leaves the cursor hidden. `summer dev` kills serve 8 seconds before serve's drain deadline. One failed open of a daily log file switches file logging off until the process exits.
## Critical Issues
### CR-01: Lines printed above a live view are reordered and can be lost
**File:** `modules/bonfire/live.go:316-323` (with `printAboveLines` at 377-387 and `runLive` at 365-372)
**Issue:** `printAboveLines` sends one `printAboveMsg` per line, and `liveModel.Update` answers each one with `tea.Println(...)`, which is a `tea.Cmd`. bubbletea v2 runs every Cmd in its own goroutine (`handleCommands`, tea.go:745-757), and that goroutine sends the resulting `printLineMessage` back into the event loop. Two consequences follow:
1. **Order is not kept.** Successive lines race each other back into the event loop.
2. **Lines can be dropped.** `runLive` sends `finishMsg` as soon as `work` returns, and `finishMsg` ends in `tea.Quit`. A print Cmd still in flight then reaches a loop that has already exited, and its line is never printed.
This affects every path that prints during a live view:
- `Output.Println` and `Printf`, which carry command data (tokens, JSON);
- status lines sent through `emit`;
- `liveProgress.Fail` failure lines (widgets.go:130-133), which are sent just before the work function returns;
- every diary console record logged while a view runs (console.go:166).
**Runtime check:** a program printing `line-00` to `line-19` with `out.Println` inside `out.Spinner` on a pty printed them out of order in 5 of 5 runs (for example `line-01 line-00 line-02 line-04 line-03 …`) and lost a line in 1 of 40 runs.
**Fix:** Deliver the lines through the event loop in order, and drain them before quitting. For example, buffer them in the model and flush them as a single ordered `tea.Sequence`; on finish, quit only after that sequence:
```go
type liveModel struct{ inner tea.Model }
func (m liveModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if lines, ok := msg.(printAboveMsg); ok {
// one message per PrintAbove call; tea.Sequence runs the prints in order
var cmds []tea.Cmd
for _, l := range strings.Split(string(lines), "\n") {
cmds = append(cmds, tea.Println(l))
}
return m, tea.Sequence(cmds...)
}
inner, cmd := m.inner.Update(msg)
m.inner = inner
return m, cmd
}
```
Ordering across separate PrintAbove calls, and draining before quit, still need one serial path. Either have `runLive` send `finishMsg` only after an acknowledgement message that is queued behind the pending prints, or keep a FIFO in the model and, on `finishMsg`, return `tea.Sequence(<pending prints>..., tea.Quit)` instead of `tea.Quit`. Send each PrintAbove call as one message (not one per line).
Add a pty test that prints N lines during each widget and asserts all N arrive in order.
### CR-02: summer delegates print a second error block and turn exit code 2 into 1
**File:** `cmd/summer/runtime.go:146-150`
**Issue:** When the app binary fails, `runAppBinary` returns `fmt.Errorf("summer: %s: %w", argv, *exec.ExitError)`. `bonfire.Execute` then:
1. renders a second ERROR block (`summer: migrate --bogus: exit status 2`) under the error block the app binary already printed;
2. maps the error to `ExitFailure`, because `isUsage` does not match an `*exec.ExitError`.
So `summer migrate --bogus` exits 1, not 2. That breaks the D-18 exit-code contract for every delegate the docs present as the normal entry point ("summer fails when the app binary fails", runtime.go:93). Nothing in `cmd/summer/runtime_test.go` covers a failing child.
**Fix:** Treat the child's failure as already reported and pass its exit code through. For example, add an exit-code carrier that `exitCode` honours:
```go
var ee *exec.ExitError
if errors.As(err, &ee) {
return bonfire.Reported(bonfire.WithExitCode(err, ee.ExitCode())) // new: exitCode() checks interface{ ExitCode() int }
}
```
### CR-03: summer delegates hide the terminal from the app and merge its stderr into stdout
**File:** `cmd/summer/runtime.go:163-168`
**Issue:** `newAppCmd` sets both `cmd.Stdout` and `cmd.Stderr` to `out`. That is the bonfire console, not an `*os.File`, so the child gets a pipe on both streams, and every byte of the child's stderr lands on summer's stdout. Since this phase that has three effects:
1. **The production confirmation is never asked.** In the child, `Interactive()` needs stdout to be a terminal. `summer migrate`, `summer migrate:rollback`, `summer deferred:purge` and `summer queue:clear` run in production without the D-28 question, even when run in a terminal.
2. **Data output gets log lines mixed in.** Records the child logs to stderr (diary records, error blocks) are mixed into its data on stdout. `summer route:list --json > routes.json` and `summer admin:token:create … --json` capture any INFO plugin boot record (deferred-items lists such records on every application command) or WARN line inside the JSON. `summer key:generate -q` captures any warning together with the key.
3. **Styled output is lost.** `summer serve` and `summer queue:work` never print their styled summaries, and the delegate's widgets never animate.
`internal/dev/watch.go:81-88` already fixes the same problem for `summer dev` by passing `os.Stdout` and `os.Stderr` on a terminal. The delegates were not given the same fix.
**Fix:** Give the child the process's own streams, and keep stderr separate:
```go
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if !bonfire.IsTerminal(out) { // tests and Call roots: keep the injected writer
cmd.Stdout, cmd.Stderr = out, os.Stderr
}
```
## Warnings
### WR-01: Ctrl+C during a spinner, progress bar or process view leaves the cursor hidden
**File:** `modules/bonfire/widgets.go:33,51`, `modules/bonfire/live.go:349-355`, `cmd/summer/main.go:28,76`, `internal/build/build.go:140`
**Issue:** The live views run with `tea.WithoutSignalHandler()` and `tea.WithInput(nil)`, and `Spinner` and `Progress` pass `context.Background()`. Neither `summer` nor the generated main installs a signal handler (`bonfire.Execute(context.Background(), …)`), so Ctrl+C terminates the process with SIGINT's default action. bubbletea hides the cursor when it starts (`ESC[?25l`, confirmed in the pty run) and only shows it again on a clean exit. `summer build`, which always shows a spinner on a terminal, and every plugin command that uses `Progress` therefore leave the user's terminal without a cursor after Ctrl+C.
**Fix:** Run `Execute` under `signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)` and give the widgets the command's context (keep it on the console in `PersistentPreRunE`). Alternatively, have `runLive` register its own SIGINT handler that calls `prog.Kill()` (which restores the terminal) and then re-raises the signal.
### WR-02: Any console's Println and Printf are sent to whichever live view is running
**File:** `modules/bonfire/output.go:192-212`, `modules/bonfire/live.go:329-339,345-373`
**Issue:** `activeProg` is a single process-wide value. `console.Println` and `Printf` call `printAboveLines` without checking that this console owns the view (`emit` checks only `w == c.out`). Any other console that prints while a view runs therefore has its data written to the terminal above that view. This includes a `bonfire.Call` root writing into a caller's `bytes.Buffer`, a nested command, or a console whose stdout is a file. For example, a `Process` loop that calls `bonfire.Call(..., &buf)` gets an empty buffer.
Two more problems sit in the same code:
- **Check-then-act race.** `liveActive()` (in `console.live`) and the registration in `runLive` are separate steps. Two widgets started concurrently both start programs, and the first one to finish sets `activeProg = nil` while the other is still running.
- **No cleanup on panic.** `runLive` unregisters without `defer`, so a panic in `work` leaves `activeProg` pointing at an orphaned program.
**Fix:** Store the owning console (or its `out` writer) together with the program, and reroute only when `c == owner`. Register under `liveMu` with a check-and-set that falls back to plain lines when a view is already active. Use `defer` for the unregister, `finishMsg` and wait steps.
### WR-03: summer dev kills serve 2 seconds after SIGTERM, but serve drains for up to 10
**File:** `internal/dev/watch.go:227-238`
**Issue:** Since this phase `summer dev` runs `bin/<app> serve`, whose shutdown drains requests and the in-process worker for up to 10 seconds and then prints the shutdown report. Before, it ran the binary with no arguments. `child.stop` still sends SIGKILL after 2 seconds. A restart or Ctrl+C that arrives during a slow request or job therefore kills serve before its report is printed (as `summer dev`'s Long help promises). Jobs that are killed remain `running` in River until its stuck-job rescuer picks them up. `cmd/summer/runtime.go:155` (`appShutdownGrace = 15s`) is longer than serve's 10-second timeout for exactly this reason.
**Fix:** Use the same grace in dev, for example `case <-time.After(appShutdownGrace):` with a shared constant longer than serve's 10-second shutdown context.
### WR-04: One failed open switches a daily log file off until the process exits
**File:** `modules/diary/daily.go:226-243`
**Issue:** When `open` fails, `disabled = true` is set and never cleared. For a long-running `serve` or `queue:work`, a transient failure at midnight rotation (ENOSPC for a moment, EMFILE during a traffic spike, a directory briefly removed by logrotate or a backup tool) stops all file logging until the next restart. Only one WARN record is written, possibly to journald. The degrade-instead-of-fail rule (T-19.8-06) was meant to keep boot from failing. A server should not lose its WARN and ERROR file trail for days because of one transient failure.
**Fix:** Disable the writer only for the date that failed, retrying on the next date (or after a backoff of about a minute), and log once per failed date:
```go
if d.disabledDate == date { d.mu.Unlock(); return len(p), nil }
if err := d.open(date); err != nil { d.disabledDate = date; … }
```
### WR-05: Error blocks, process views and tables write untrusted text to the terminal without stripping escape sequences
**File:** `modules/bonfire/errors.go:145-169`, `modules/bonfire/live.go:286-289`, `modules/bonfire/widgets.go:142-166`
**Issue:** T-19.8-03 strips control characters only in diary's console handler. `RenderError` passes messages, explanations and the `-v` cause chain through `sunscreen.Scrub` only, and Scrub redacts credential shapes but leaves ESC and other control characters in place. The process view prints `it.err.Error()` and item names unchanged, and `Table` prints cell values unchanged. When the profile allows color, `colorprofile.Writer` passes those sequences through.
The text can come from outside the operator's control: database or remote-service error text (the centrifugo health error, upstream HTTP bodies), and admin-supplied token names shown by `admin:token:list`. Such text can move the cursor, rewrite earlier lines or set the window title on the operator's terminal.
**Fix:** Export diary's `sanitize` (for example as `bonfire.Sanitize`, with diary calling it) and apply it to every error-block line, process-view item and error, and table cell before styling.
### WR-06: Setup and ApplyGlobals close daily files that other goroutines may still be writing through
**File:** `modules/diary/diary.go:156-179,216-227`
**Issue:** `install` swaps the state and then closes the old channels' daily writers. A goroutine that loaded the old state just before the swap still writes through the old `dailyWriter`. Its `Write` finds `file == nil` and reopens the file (`daily.go:233`). Nothing ever closes that writer again, so the descriptor stays open until the GC finalizer closes it.
`ApplyGlobals` runs in `PersistentPreRunE`, after plugins have booted. By then plugin goroutines (cache, realtime, the worker) may already be logging, so the window is real.
There is also a check-then-act race: `ApplyGlobals` reads `current` outside `mu`, so two concurrent calls can both rebuild the channels.
**Fix:** Mark the old writers closed for good (`d.closed = true`, with `Write` returning `len(p), nil` once it is set) and do not reopen them. Alternatively, reference-count the old state and close it after in-flight `Handle` calls finish. Take `mu` for the whole of `ApplyGlobals`.
### WR-07: PendingMigrations keeps its own copy of Migrate's migration sets
**File:** `modules/lagoon/migrations.go:221-245` vs `modules/lagoon/migrations.go:64-115`; `modules/lagoon/commands.go:139-171`
**Issue:** `migrationSets` re-lists the framework sets and the plugin loop by hand, and `Migrate` does not use it. When a set is added to `Migrate` (as the queue and deferred sets were), several things go wrong without any failing test:
- the serve startup summary,
- `migrate --pretend`,
- the production question's count,
- the "N applied" lines,
- `migrate:status --json`.
The "applied" lines are also printed from the list computed before `Migrate` ran, not from what it actually applied.
**Fix:** Make `Migrate` iterate over `migrationSets(gdb, plugins)`, so both functions read one list. Optionally, recompute the pending list after `Migrate` and report the difference.
### WR-08: The wrong-directory hint repeats every argument, including a --password value, on stderr
**File:** `modules/bonfire/errors.go:220-224` (via `Fail`, `modules/bonfire/execute.go:346-351`)
**Issue:** When an application binary fails outside its root, `wrongDirectory` builds the Try line from all of `os.Args[1:]`. For example, `cd bin && ./acme admin:create --email a@b.c --password hunter2` fails at `compass.Open`, and the error block prints `cd ..; bin/acme admin:create --email a@b.c --password hunter2`. The deprecated `--password` flag is still accepted, so a password that was only in the shell history now also reaches CI and deploy logs that capture stderr. `sunscreen.Scrub` does not catch it, because it recognises only Bearer, `sk-` and `x-api-key` shapes.
**Fix:** Mask the values of flags whose names look like secrets (`password`, `secret`, `token`, `key`) in the Try line (`--password=***`). Alternatively, print only the command word (`cd ..; bin/acme admin:create …`).
## Info
### IN-01: A forced-color console drops HideOnTTY records even when stderr is not a terminal
**File:** `modules/diary/console.go:117-122`
**Issue:** The styled `consoleHandler` drops every `HideOnTTY` record unconditionally. It is also used when color is forced (`--ansi`, `FORCE_COLOR`) on a non-terminal stderr. With stdout on a terminal and stderr redirected (`FORCE_COLOR=1 bin/acme serve 2>serve.log`), the "startup summary" and "listening on" records never reach the file, although the HideOnTTY docs promise that only a terminal console skips them.
**Fix:** Check `isTerminal(w)` once when the handler is built, and drop the records only when it is true (the same rule `ttyFilter` uses).
### IN-02: PeekGlobals and cobra disagree on some argument forms
**File:** `modules/bonfire/globals.go:52-81`
**Issue:** `--quiet=true`, `--no-interaction=true`, `--verbose=2` and `--ansi=false` are ignored by `PeekGlobals` but honoured by pflag. A command flag value that looks like a global (`--name -q`) is the reverse: pflag takes it as the flag's value, `PeekGlobals` reads it as a global. The pre-boot diary setup, the error block's `-v` chain and passthrough globals can therefore differ from what the command runs with.
**Fix:** Parse `--flag=value` forms with `strconv.ParseBool` and `strconv.Atoi`, or re-parse the args with a throwaway pflag FlagSet that holds only the global flags (`ParseErrorsWhitelist.UnknownFlags = true`).
### IN-03: isUsage recognises cobra's unknown-command error by its text
**File:** `modules/bonfire/errors.go:41-47,119`
**Issue:** Any error whose message starts with "unknown command" exits 2 and is rewritten as `Command "…" is not defined.`. A plugin error worded that way would be misreported.
**Fix:** Turn cobra's error into a `UsageError` in one place (for example a root `Args` or `ValidArgsFunction` check, or by matching it once in `Execute` before rendering), and test it with `errors.As`.
### IN-04: The --json flag is read two different ways
**File:** `modules/cabana/token_commands.go:150,217,299`, `modules/lighthouse/centrifugo/commands.go`, `modules/surf/routelist_command.go` vs `modules/lagoon/commands.go` (`flagOn`)
**Issue:** cabana, centrifugo and route:list compare the value with `== "true"`, while lagoon uses `strconv.ParseBool`. As a result `--json=1` works for `migrate:status` but not for `admin:token:list`.
**Fix:** Add one bonfire helper (for example `bonfire.FlagBool(in, name)`) and use it everywhere.
### IN-05: summer's scaffolding result lines are now hidden by -q
**File:** `cmd/summer/main.go:126,334,401`
**Issue:** `created <path>` and `added <dir>` used to print through `Printf`. They now print through `Success` as `Created …` and `Added …`, so `-q` hides them and their wording changed. Scripts that read the created paths stop working without notice.
**Fix:** Print the created paths with `Println` as data (D-27), or mention the change in `docs/setup/upgrading.md`.
### IN-06: fang replaces the root help function that NewRootIO installs
**File:** `modules/bonfire/root.go:139-141`, `modules/bonfire/execute.go:327-337`
**Issue:** `fang.Execute` calls `root.SetHelpFunc(fang's helpFunc)` (fang.go:140), so under `Execute` bonfire's root help function is dead code. Today `Execute` strips `-h` and `--help` when there is no command word, so the replacement is not reachable. A future path that calls the root's help would print fang's layout, not the Artisan list.
**Fix:** Set the root help function again after `fang.Execute` sets its own (not possible from outside), or drop fang and call `root.ExecuteContext` with `RenderError` directly. bonfire already turns off every fang feature except the error handler.
### IN-07: diary pulls the whole TUI stack into security modules
**File:** `modules/diary/diary.go:21-22`, `modules/bouncer/turnstile.go`, `modules/wristband/oidc_token.go`
**Issue:** `diary` imports `bonfire`, which imports cobra, fang, bubbletea, bubbles and huh. bouncer's README now says its "only framework dependency is diary", but in practice the security primitive links the console UI stack. That widens the code a security review of bouncer and wristband has to cover.
**Fix:** Move `ColorMode`, `DetectProfile`, `Palette` and `PrintAbove` into a small leaf package (or into diary itself) so that diary does not import bonfire.
### IN-08: The console sanitizer passes raw C1 bytes through on its fast path
**File:** `modules/diary/console.go:278-288`
**Issue:** A lone `0x9b` byte (8-bit CSI) is invalid UTF-8. `range` decodes it as `U+FFFD`, which is not a control character, so `sanitize` returns the original string with the raw byte in it. Terminals that interpret 8-bit C1 controls then see an escape sequence.
**Fix:** Run `strings.ToValidUTF8(s, "<22>")` before the control-character scan.
### IN-09: The phase gate names consuming applications
**File:** `scripts/check-phase19.8.sh:71-77,229`
**Issue:** The script hard-codes `sm-demo-app`, `sm-bm-app`, `sm-grzybyfunkcjonalne-app` and `sm-summercmsio-app`, and splits a consumer's name into two strings. CLAUDE.md says framework code never names a consuming application. Earlier phase gates do the same, so the precedent exists, but this framework repository is published.
**Fix:** Read the sibling paths from environment variables only, with no defaults, or move the cross-repository stages to a gate outside the framework repository.
---
_Reviewed: 2026-10-21T22:33:36Z_
_Reviewer: Claude (msd-code-reviewer)_
_Depth: standard_

View File

@@ -7,3 +7,26 @@ Out-of-scope findings recorded during execution. Not fixed in this phase.
- **Flaky wristband test.** `TestOIDCSubjectIsRandomSmoke` (`modules/wristband/oidc_security_test.go:151`) rejects a subject that contains `ada` (to catch the fixture user's name), but a random UUIDv4 contains `ada` in about 0.7% of runs. It failed once during the 19.8-01 short suite (`sub = "64a89757-94a7-4699-bf28-4329d8c7eada"`) and passed on three reruns. Fix: compare against the fixture's name as a whole, or check that the subject differs from it, instead of matching a substring of hex.
- **Pre-existing gofmt drift.** `gofmt -l` lists `internal/build/registry.go`, `modules/cabana/account_tokens.go` (no newline at end of file) and some test files (`modules/cabana/*_test.go`, `modules/compass/*_test.go`, `modules/tide/*_test.go`). Unchanged by this plan beyond the logger lines.
- **examples/hello go.mod is stale outside the workspace.** `GOWORK=off go -C examples/hello build .` asks for `go mod tidy`: its go.mod lacks requirements the framework already had before this phase (aws-sdk, bluemonday, redis, among others). Inside the repository's go.work every build and test passes, so the example modules' go.mod and go.sum were left as they were.
## Found during 19.8-03 (segment B, plugin sweep)
Each failure below also fails at the plugin's commit before the sweep, with framework master and with framework v0.3.1 (checked in scratch worktrees), so it is not caused by the sweep or by v0.4.0. Not fixed here.
- **sm-sitemap-plugin `TestSitemapAdminSmoke`** (`sitemap_smoke_test.go:607`): expects the phrase key `golem15.sitemap::lang.item.reference_required` in a 422 body, gets the translated text "The item reference is required.".
- **sm-faq-plugin `TestFaqAdminSmoke`, `TestFaqAdminPurgesSiteCache`** (`site_cache_admin_test.go:208`): a create without a locale returns "Choose a language." where the test expects the phrase key.
- **sm-menu-plugin `TestMenuAdminSmoke`, `TestMenuAdminPurgesSiteCache`** (`site_cache_admin_test.go:228`): a create returns 422 "Choose a menu or a parent entry." where the test expects success or the phrase key.
- **sm-user-plugin `updates` migration tests** (`TestExtendUsersMigration`, `TestCreateUserThrottleMigration`, `TestCreateJwtBlacklistMigration`, `user_session_test.go`): "survived rollback", a varying subset per run; also at v0.1.5 with framework v0.3.1.
- **Plugin boot logs on `list`.** Application binaries print INFO boot records (for example `paymentgateway: operators registered`) before `list` and `help` output. Already captured by another session as a todo (summercms.go `c1f5ded`, "Quiet plugin boot logs on console commands like --help").
- **sm-demo-app roadmap snapshot vs local master.** `scripts/sync-roadmap.sh --check` fails against local framework master while other sessions' 19.6 and 19.6.1 planning commits are unpushed; it passes with `ROADMAP_REF=origin/master`. Re-sync after those commits are pushed.
## Found during 19.8-04 (unit tests)
Recorded, not fixed here: none of them is a defect in released behaviour.
- **gofmt drift from plan 03.** `gofmt -l` lists `cmd/summer/main.go`, `cmd/summer/runtime.go`, `cmd/summer/runtime_test.go` (map key alignment) and `modules/flare/commands.go`; each already differs at the plan 03 release. The tests added here are formatted.
- **Dotted component names in YAML.** The diary README and `docs/services/logging.md` say component names cannot contain dots. compass keeps a dotted YAML key literal (`components: {golem15.blog: debug}` parses as component `golem15.blog` and works for `diary.For("golem15.blog")`); only the nested form, which `SUMMER_LOG__` variables produce, is refused. `TestConfigRules` pins both. Either reject the literal form or reword the docs ("use names without dots so environment overrides can reach them").
- **`Version.Header` collapses equal versions.** An application built as `devel` against a `devel` framework prints `SummerCMS acme devel` without `(framework devel)`, the same rule that gives `SummerCMS summer v0.4.0`. Go stamps VCS pseudo-versions on applications, so it shows only with `-buildvcs=false`. Pinned by `TestVersionHeader`.
- **Error blocks scrub credential shapes only.** `RenderError` passes text through `sunscreen.Scrub` (Bearer tokens, `sk-` keys, `x-api-key:`); a `password=...` written into an error message is printed as is. The plan's `TestErrorScrubbed` wording ("a password attribute in the message") is covered for the shapes Scrub knows; extending Scrub is a sunscreen change for every log line and needs its own decision.
- **Live views on a non-terminal writer draw no frames.** `runLive` takes the window size from the terminal; with a buffer the renderer draws nothing but the PrintAbove lines. The widget matrix therefore proves the live path ran (cursor hidden, no fallback lines) and `TestLiveModels` renders the frames with a fixed size; a pty run of spinner, progress and process view printed every final frame (`✓ fast spinner`, `4/4`, `✓ a`, `✗ b bad row`, `Importing 2/2 … 100%`).
- **Landing end-to-end check.** `TestTerminalCommands` (sm-summercmsio-app) runs the deployed one-liner and needs network, Docker and ports 54320 and 8080; the gate runs it only with `SUMMERCMS_TERMINAL_CHECK=1` and prints NOT RUN otherwise.
- **Stray binary in sm-grzybyfunkcjonalne-app.** An untracked `sm-grzybyfunkcjonalne-app` executable sits in that application's root (present before this plan; a `go build` without `-o`). Not this plan's; left alone.

View File

@@ -0,0 +1,41 @@
---
created: 2026-10-21T13:33:21.495Z
title: Quiet plugin boot logs on console commands like --help
area: framework
severity: minor
g15office: izelucbh
files:
- ../sm-demo-app/plugins/golem15/paymentgateway/boot.go:129
- ../sm-demo-app/plugins/golem15/paymentgatewaysubscriptions/boot.go:86
- modules/diary
- modules/bonfire
---
## Problem
Every console command prints the plugins' boot-time INFO records, even `--help`. From sm-demo-app, after phase 19.8 plans 01–03:
```
$ bin/demo serve --help
time=2026-10-21T15:31:35.392+02:00 level=INFO msg="paymentgateway: operators registered" codes="[golem15.paymentgateway::lang.channels.cash golem15.paymentgateway::lang.channels.manualTransfer golem15.pgstripe::lang.operators.stripe]"
time=2026-10-21T15:31:35.394+02:00 level=INFO msg="golem15.paymentgatewaysubscriptions: registries collected" subscriber_types=[user] gateways=[stripe]
Serve the application HTTP API
```
Registration chatter belongs to `serve` (or `-v`), not to `--help`, `list` or a one-off command.
Two causes:
1. sm-paymentgateway-plugin (`boot.go:129`) and sm-paymentgatewaysubscriptions-plugin (`boot.go:86`) log through `slog.Default().Info`, not a diary component logger. That is why the lines are plain slog text (`time=… level=INFO msg=…`) and not diary console output. Plan 19.8-01 moved only the framework modules onto diary components, not the shared plugins.
2. Plugins `Boot` before bonfire dispatches the command, so whatever a plugin logs at Info level during boot shows on every command.
Not critical; found 2026-10-21 during phase 19.8.
## Solution
TBD. Options, possibly combined:
- In the payment plugins, log these records at Debug through `diary.For("<component>")`. These are shared core plugins, so the change must not break anything.
- Check whether diary installs itself as `slog.Default` early enough. If it did, these lines would at least be formatted and levelled through diary.
- Let bonfire handle `--help`, `help` and `list` before plugin `Boot`, or give non-long-running commands a default console level that hides Info boot records unless `-v` is set.
- Sweep the other shared plugins for `slog.Default()` calls in `Register`/`Boot`.

View File

@@ -0,0 +1,98 @@
---
created: 2026-10-22T00:43:48.000Z
title: Console review info findings (IN-01..IN-09)
area: framework
severity: minor
g15office: pending
files:
- modules/diary/console.go
- modules/diary/diary.go
- modules/bonfire/globals.go
- modules/bonfire/errors.go
- modules/bonfire/root.go
- modules/bonfire/execute.go
- modules/cabana/token_commands.go
- modules/lighthouse/centrifugo/commands.go
- modules/surf/routelist_command.go
- modules/lagoon/commands.go
- cmd/summer/main.go
- modules/bouncer/turnstile.go
- modules/wristband/oidc_token.go
- scripts/check-phase19.8.sh
---
## Context
The 19.8 code review (`.planning/phases/19.8-console-commands-overhaul/19.8-REVIEW.md`, v0.3.1..v0.4.1) found 3 critical issues, 8 warnings and 9 info findings. Plan 19.8-05 fixed the critical issues and the warnings. The nine info findings below are deferred to this todo. Line numbers are the review's; locate the code by symbol.
## IN-01: A forced-color console drops HideOnTTY records even when stderr is not a terminal
**File:** `modules/diary/console.go` (`consoleHandler`, the HideOnTTY check)
**Problem:** The styled `consoleHandler` drops every `HideOnTTY` record. It is also used when color is forced (`--ansi`, `FORCE_COLOR`) on a non-terminal stderr. So with stdout on a terminal and stderr redirected (`FORCE_COLOR=1 bin/acme serve 2>serve.log`), the "startup summary" and "listening on" records never reach the file. The HideOnTTY docs promise that only a terminal console skips them.
**Suggested fix:** Check `isTerminal(w)` once when the handler is built, and drop the records only when it is true (the rule `ttyFilter` already uses).
## IN-02: PeekGlobals and cobra disagree on some argument forms
**File:** `modules/bonfire/globals.go` (`PeekGlobals`)
**Problem:** `PeekGlobals` ignores `--quiet=true`, `--no-interaction=true`, `--verbose=2` and `--ansi=false`, but pflag honours them. It also reads a command flag value that looks like a global (`--name -q`) as a global, where pflag takes it as the flag's value. As a result the pre-boot diary setup, the error block's `-v` chain and the passthrough globals can differ from what the command runs with.
**Suggested fix:** Parse the `--flag=value` forms with `strconv.ParseBool` and `strconv.Atoi`, or re-parse the args with a throwaway pflag FlagSet that holds only the global flags (`ParseErrorsWhitelist.UnknownFlags = true`).
## IN-03: isUsage recognises cobra's unknown-command error by its text
**File:** `modules/bonfire/errors.go` (`isUsage`, `RenderError`)
**Problem:** Any error whose message starts with "unknown command" exits 2 and is rewritten as `Command "…" is not defined.`. A plugin error worded that way would be misreported.
**Suggested fix:** Turn cobra's error into a `UsageError` in one place (a root `Args` or `ValidArgsFunction` check, or match it once in `Execute` before rendering), and test it with `errors.As`.
## IN-04: The --json flag is read two different ways
**Files:** `modules/cabana/token_commands.go`, `modules/lighthouse/centrifugo/commands.go`, `modules/surf/routelist_command.go` vs `modules/lagoon/commands.go` (`flagOn`)
**Problem:** cabana, centrifugo and route:list compare the value with `== "true"`, while lagoon uses `strconv.ParseBool`. So `--json=1` works for `migrate:status` but not for `admin:token:list`.
**Suggested fix:** Add one bonfire helper (for example `bonfire.FlagBool(in, name)`) and use it everywhere. It would be an additive API, so plugins keep building.
## IN-05: summer's scaffolding result lines are now hidden by -q
**File:** `cmd/summer/main.go` (the scaffolding commands' `Created` and `Added` lines)
**Problem:** `created <path>` and `added <dir>` used to print through `Printf`. They now print through `Success` as `Created …` and `Added …`, so `-q` hides them and their wording changed. Scripts that read the created paths stop working without notice.
**Suggested fix:** Print the created paths with `Println` as data (D-27), or mention the change in `docs/setup/upgrading.md`.
## IN-06: fang replaces the root help function that NewRootIO installs
**Files:** `modules/bonfire/root.go`, `modules/bonfire/execute.go`
**Problem:** `fang.Execute` calls `root.SetHelpFunc` with fang's own help function, so under `Execute` bonfire's root help function is dead code. Today it is unreachable, because `Execute` strips `-h` and `--help` when no command word is given. A future path that calls the root's help would print fang's layout instead of the Artisan list.
**Suggested fix:** Drop fang and call `root.ExecuteContext` with `RenderError` directly (bonfire already turns off every fang feature except the error handler), or restore the root help function after fang sets its own.
## IN-07: diary pulls the whole TUI stack into security modules
**Files:** `modules/diary/diary.go`, `modules/bouncer/turnstile.go`, `modules/wristband/oidc_token.go`
**Problem:** `diary` imports `bonfire`, which imports cobra, fang, bubbletea, bubbles and huh. So bouncer and wristband, which log through diary, link the console UI stack, and a security review of them has more code to cover.
**Suggested fix:** Move `ColorMode`, `DetectProfile`, `Palette`, `PrintAbove` and now `Sanitize` into a small leaf package (or into diary itself), so diary no longer imports bonfire. Since plan 19.8-05, diary's console channel also calls `bonfire.Sanitize` (WR-05). Keep `bonfire.Sanitize` as the exported entry point when the leaf package is introduced, because it is public API since v0.4.2.
## IN-08: The console sanitizer passes raw C1 bytes through on its fast path
**File:** `modules/bonfire/errors.go` (`bonfire.Sanitize`, formerly diary's `sanitize`)
**Problem:** A lone `0x9b` byte (8-bit CSI) is invalid UTF-8. `range` decodes it as `U+FFFD`, which is not a control character, so the function returns the original string with the raw byte in it. Terminals that interpret 8-bit C1 controls then see an escape sequence.
**Suggested fix:** Since plan 19.8-05 the sanitizer is `bonfire.Sanitize`, used by error blocks, process views, tables and diary's console. Run `strings.ToValidUTF8(s, "<22>")` before the control-character scan there, and add the case to `TestSanitize`.
## IN-09: The phase gate names consuming applications
**File:** `scripts/check-phase19.8.sh` (the `DEMO_APP`, `BM_APP`, `GRZYBY_APP` and `SUMMERCMSIO_APP` defaults)
**Problem:** The script hard-codes `sm-demo-app`, `sm-bm-app`, `sm-grzybyfunkcjonalne-app` and `sm-summercmsio-app`, and splits a consumer's name into two strings to get past the name check. CLAUDE.md says framework code never names a consuming application. Earlier phase gates do the same, but this framework repository is published.
**Suggested fix:** Read the sibling paths from environment variables only, with no defaults, or move the cross-repository stages to a gate outside the framework repository.

View File

@@ -11,9 +11,9 @@ import (
"path/filepath"
"strings"
"syscall"
"time"
"git.golem15.com/golem15/summercms/internal/build"
"git.golem15.com/golem15/summercms/internal/dev"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
@@ -90,7 +90,7 @@ func delegateCommand(name, description, hint string) bonfire.Command {
long := description + ".\n\nsummer runs bin/<binary> " + name +
" in the application directory, building the binary first when it is missing. Every flag and argument after " +
name + ", including --help, is passed to the app binary unchanged, and so are the global options (-v, -q, --ansi, --no-ansi, -n, --env). " +
"summer fails when the app binary fails."
"When the app binary fails, summer exits with its exit code."
if hint != "" {
long += "\n\nCommon flags: " + hint + "."
}
@@ -141,30 +141,60 @@ func runAppBinary(ctx context.Context, argv []string, out bonfire.Output) error
return fmt.Errorf("summer: refusing to recurse into %s", bin)
}
}
cmd := newAppCmd(ctx, bin, appDir, argv, out)
stdout, stderr := appStreams(out, bonfire.IsTerminal(out))
return runAppCmd(newAppCmd(ctx, bin, appDir, argv, stdout, stderr), argv)
}
// appStreams picks the app binary's stdout and stderr. On a terminal
// (terminal is bonfire.IsTerminal(out)) the binary gets summer's own
// streams, so it sees the terminal: it asks the production question, prints
// styled summaries and animates its widgets. Otherwise its stdout goes
// through out (tests and bonfire.Call roots capture it) and its stderr to
// summer's stderr, so log lines and error blocks never mix into data such
// as --json output.
func appStreams(out bonfire.Output, terminal bool) (stdout, stderr io.Writer) {
if terminal {
return os.Stdout, os.Stderr
}
return out, os.Stderr
}
// runAppCmd runs cmd, the app binary started with argv, forwarding SIGINT
// and SIGTERM to it. When the binary exits with a code, the error carries
// that code (bonfire.WithExitCode) and is marked as already reported.
func runAppCmd(cmd *exec.Cmd, argv []string) error {
sigs := make(chan os.Signal, 2)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigs)
if err := runChild(cmd, sigs); err != nil {
return fmt.Errorf("summer: %s: %w", strings.Join(argv, " "), err)
err = fmt.Errorf("summer: %s: %w", strings.Join(argv, " "), err)
var exit *exec.ExitError
if errors.As(err, &exit) && exit.Exited() {
// The binary printed its own error; summer exits with its
// code and prints nothing more. A binary ended by a signal
// printed nothing, so it gets summer's error block.
return bonfire.Reported(bonfire.WithExitCode(err, exit.ExitCode()))
}
return err
}
return nil
}
// appShutdownGrace is how long a cancelled app binary may take to exit after
// SIGTERM before it is killed. It is longer than the 10-second shutdown
// timeout of serve and the workers, so they finish a graceful shutdown.
const appShutdownGrace = 15 * time.Second
// SIGTERM before it is killed: the grace summer dev gives serve as well,
// longer than the 10-second shutdown timeout of serve and the workers.
const appShutdownGrace = dev.ShutdownGrace
// newAppCmd builds the command that runs the app binary bin with argv in dir.
// The child inherits summer's stdin, so the hidden password prompts of
// admin:create and admin:reset-password read from the terminal. Cancelling
// ctx sends SIGTERM, and the child is killed only after appShutdownGrace.
func newAppCmd(ctx context.Context, bin, dir string, argv []string, out io.Writer) *exec.Cmd {
// newAppCmd builds the command that runs the app binary bin with argv in
// dir, writing to stdout and stderr (see appStreams). The child inherits
// summer's stdin, so the hidden password prompts of admin:create and
// admin:reset-password read from the terminal. Cancelling ctx sends
// SIGTERM, and the child is killed only after appShutdownGrace.
func newAppCmd(ctx context.Context, bin, dir string, argv []string, stdout, stderr io.Writer) *exec.Cmd {
cmd := exec.CommandContext(ctx, bin, argv...)
cmd.Dir = dir
cmd.Stdout = out
cmd.Stderr = out
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
cmd.Cancel = func() error {

View File

@@ -5,9 +5,11 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"slices"
"strings"
@@ -189,7 +191,7 @@ func TestRunChildForwardsSignals(t *testing.T) {
for _, sig := range []os.Signal{os.Interrupt, syscall.SIGTERM} {
t.Run(sig.String(), func(t *testing.T) {
var out syncBuffer
cmd := newAppCmd(context.Background(), os.Args[0], ".", helperArgv(), &out)
cmd := newAppCmd(context.Background(), os.Args[0], ".", helperArgv(), &out, &out)
helperEnv(cmd)
sigs := make(chan os.Signal, 1)
done := make(chan error, 1)
@@ -218,7 +220,7 @@ func TestNewAppCmdCancelSendsSIGTERM(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var out syncBuffer
cmd := newAppCmd(ctx, os.Args[0], ".", helperArgv(), &out)
cmd := newAppCmd(ctx, os.Args[0], ".", helperArgv(), &out, &out)
helperEnv(cmd)
done := make(chan error, 1)
go func() { done <- runChild(cmd, nil) }()
@@ -236,3 +238,178 @@ func TestNewAppCmdCancelSendsSIGTERM(t *testing.T) {
t.Fatalf("child was not stopped with SIGTERM:\n%s", out.String())
}
}
// TestDelegateExamples: every delegate has two examples rendered after
// summer in its help, and its long help says that flags and the global
// options pass to the app binary; global options before or after the
// command word reach the app binary.
func TestDelegateExamples(t *testing.T) {
got := captureAppRunner(t)
for _, c := range delegateCommands() {
if len(c.Examples) != 2 {
t.Errorf("%s has %d examples, want 2", c.Name, len(c.Examples))
}
if !strings.Contains(c.Long, "global options (-v, -q, --ansi, --no-ansi, -n, --env)") || !strings.Contains(c.Long, "passed to the app binary unchanged") {
t.Errorf("%s: long help does not say that global options pass through:\n%s", c.Name, c.Long)
}
out, err := runSummer(t, "help", c.Name)
if err != nil {
t.Fatalf("help %s: %v", c.Name, err)
}
for _, e := range c.Examples {
if !strings.HasPrefix(e.Command, c.Name) || e.Description == "" {
t.Errorf("%s: example %+v does not start with the command or lacks a description", c.Name, e)
}
if !strings.Contains(out, " summer "+e.Command+"\n") || !strings.Contains(out, "# "+e.Description) {
t.Errorf("help %s lacks the example %q:\n%s", c.Name, e.Command, out)
}
}
}
if *got != nil {
t.Fatalf("help ran the app binary with %q", *got)
}
if problems := bonfire.CheckDocs(delegateCommands()); len(problems) != 0 {
t.Errorf("delegates lack documentation: %v", problems)
}
for _, tc := range []struct {
args []string
want []string
}{
{[]string{"-v", "migrate", "--pretend"}, []string{"migrate", "-v", "--pretend"}},
{[]string{"migrate", "-qn"}, []string{"migrate", "-qn"}},
{[]string{"--env", "staging", "queue:work"}, []string{"queue:work", "--env", "staging"}},
{[]string{"--no-ansi", "serve", "--addr", ":8123"}, []string{"serve", "--no-ansi", "--addr", ":8123"}},
} {
got := captureAppRunner(t)
if out, err := runSummer(t, tc.args...); err != nil {
t.Fatalf("%q: %v\n%s", tc.args, err, out)
}
if !slices.Equal(*got, tc.want) {
t.Errorf("%q: argv = %q, want %q", tc.args, *got, tc.want)
}
}
}
// TestExitHelperProcess is not a real test: TestDelegateExitCodePassthrough
// re-executes the test binary with SUMMER_EXIT_HELPER set to get a child
// that prints an error on stderr and exits 2, or that is killed by SIGKILL.
func TestExitHelperProcess(t *testing.T) {
switch os.Getenv("SUMMER_EXIT_HELPER") {
case "exit2":
fmt.Fprintln(os.Stderr, "ERROR the app binary failed")
os.Exit(2)
case "sigkill":
p, _ := os.FindProcess(os.Getpid())
_ = p.Signal(syscall.SIGKILL)
time.Sleep(10 * time.Second)
os.Exit(3)
}
}
// exitHelperRunner replaces appRunner with one that runs the exit helper
// as the app binary.
func exitHelperRunner(t *testing.T, mode string) {
t.Helper()
prev := appRunner
appRunner = func(ctx context.Context, argv []string, out bonfire.Output) error {
stdout, stderr := appStreams(out, bonfire.IsTerminal(out))
cmd := newAppCmd(ctx, os.Args[0], ".", []string{"-test.run=^TestExitHelperProcess$"}, stdout, stderr)
cmd.Env = append(os.Environ(), "SUMMER_EXIT_HELPER="+mode)
return runAppCmd(cmd, argv)
}
t.Cleanup(func() { appRunner = prev })
}
// TestDelegateExitCodePassthrough (CR-02): summer exits with the app
// binary's exit code and prints no error block of its own; a binary killed
// by a signal gets summer's error block and exit 1.
func TestDelegateExitCodePassthrough(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("SIGKILL is POSIX-only")
}
run := func(t *testing.T, mode string) (string, string, int) {
t.Helper()
exitHelperRunner(t, mode)
var out, errW syncBuffer
root, err := bonfire.NewRootIO("summer", toolCommands(), strings.NewReader(""), &out, &errW)
if err != nil {
t.Fatal(err)
}
code := bonfire.Execute(context.Background(), root, []string{"migrate", "--bogus"})
return out.String(), errW.String(), code
}
t.Run("exit code", func(t *testing.T) {
out, errOut, code := run(t, "exit2")
if code != 2 {
t.Errorf("exit %d, want the binary's 2", code)
}
for _, s := range []string{out, errOut} {
if strings.Contains(s, " ERROR ") || strings.Contains(s, "summer: migrate") {
t.Errorf("summer printed an error block of its own:\nstdout %q\nstderr %q", out, errOut)
break
}
}
})
t.Run("killed by a signal", func(t *testing.T) {
out, errOut, code := run(t, "sigkill")
if code != bonfire.ExitFailure {
t.Errorf("exit %d, want 1", code)
}
if n := strings.Count(errOut, " ERROR "); n != 1 || !strings.Contains(errOut, "summer: migrate --bogus: signal: killed") {
t.Errorf("want one error block naming the signal, got %d:\nstdout %q\nstderr %q", n, out, errOut)
}
})
}
// TestDelegateStreams (CR-03): off a terminal the app binary's stdout goes
// through summer's Output and its stderr to summer's stderr, never into
// summer's stdout.
func TestDelegateStreams(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake app binary is a shell script")
}
app := t.TempDir()
if err := os.WriteFile(filepath.Join(app, "summer.yaml"), []byte("module: example.com/acme\nbinary: acme\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(app, "bin"), 0o755); err != nil {
t.Fatal(err)
}
script := "#!/bin/sh\necho data-line\necho log-line >&2\n"
if err := os.WriteFile(filepath.Join(app, "bin", "acme"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Chdir(app)
out, err := runSummer(t, "route:list", "--json")
if err != nil {
t.Fatalf("route:list: %v\n%s", err, out)
}
if !strings.Contains(out, "data-line") {
t.Errorf("the binary's stdout did not reach summer's Output: %q", out)
}
if strings.Contains(out, "log-line") {
t.Errorf("the binary's stderr landed in summer's stdout: %q", out)
}
}
// TestAppStreams (CR-03): on a terminal the app binary gets summer's own
// stdout and stderr; off a terminal its stdout goes through the Output and
// its stderr to summer's stderr.
func TestAppStreams(t *testing.T) {
var buf bytes.Buffer
out := bonfire.NewOutput(strings.NewReader(""), &buf, &buf)
if bonfire.IsTerminal(out) {
t.Fatal("a buffer console reports a terminal")
}
stdout, stderr := appStreams(out, true)
if stdout != os.Stdout || stderr != os.Stderr {
t.Errorf("terminal: streams %v, %v; want os.Stdout, os.Stderr", stdout, stderr)
}
stdout, stderr = appStreams(out, false)
if stdout != io.Writer(out) || stderr != os.Stderr {
t.Errorf("not a terminal: streams %v, %v; want the Output, os.Stderr", stdout, stderr)
}
}

View File

@@ -141,6 +141,10 @@ summer -v migrate --pretend
summer admin:token:create jane --scope journal.posts --json
```
The binary writes to your terminal directly, so it asks the production question, prints its styled startup summary and animates its spinners as if you had run it yourself. Its error stream stays separate from its output: `summer route:list --json > routes.json` captures only the JSON, while warnings and log lines still reach the terminal on stderr.
When the binary fails, `summer` exits with the binary's exit code (a usage error stays `2`) and prints nothing more: the binary has already printed its error. A binary killed by a signal gets an error block from `summer` and exit code `1`.
When `summer` receives SIGINT or SIGTERM while the binary runs, it passes the signal on and waits for the binary to shut down, so stopping `summer serve` with Ctrl+C or `kill` frees its port.
`summer` does not rebuild an existing binary before it delegates. Run `summer build` after you change code, or keep `summer dev` running. Every other runtime command, such as `deferred:purge` or the commands your plugins add, is run on the binary directly.

View File

@@ -20,7 +20,7 @@ summer build
summer dev
```
`summer dev` passes its global options on to `serve`: `summer dev -v` shows the per-plugin lines of the startup summary and every component's info logs, `summer dev -q` only warnings and errors. Ctrl+C stops `serve`, which prints its shutdown report, then `summer dev` prints its own report (rebuilds, failed builds, restarts, uptime) and exits 0.
`summer dev` passes its global options on to `serve`: `summer dev -v` shows the per-plugin lines of the startup summary and every component's info logs, `summer dev -q` only warnings and errors. Ctrl+C stops `serve` and waits for its shutdown report: `serve` drains the requests in flight for up to 10 seconds, and `summer dev` kills it only if it is still running 15 seconds after the signal (a restart after a rebuild waits the same way). Then `summer dev` prints its own report (rebuilds, failed builds, restarts, uptime) and exits 0.
Do not edit `main.go` or `plugins.gen.go`: `summer build` rewrites them from the manifest.

View File

@@ -152,7 +152,7 @@ The framework's own commands follow these rules, and so should a plugin's:
## Errors and hints
When `Run` returns an error, the binary prints a block on stderr: the ERROR badge and the message, then "Run with -v for details.". With `-v` the block lists the wrapped errors under "Caused by:" instead. Messages pass through `sunscreen.Scrub`, so a token inside an error text is not printed.
When `Run` returns an error, the binary prints a block on stderr: the ERROR badge and the message, then "Run with -v for details.". With `-v` the block lists the wrapped errors under "Caused by:" instead. Messages pass through `sunscreen.Scrub`, so a token inside an error text is not printed, and through `bonfire.Sanitize`, which drops control characters: an error text from a database or a remote service cannot move the cursor, clear the screen or retitle the terminal. Table cells and the item names and errors of `bonfire.Process` are cleaned the same way.
Wrap an error with `bonfire.WithHint` to explain it and to say what to run next. `{bin}` in the Try text becomes `bin/<name>` or `summer`, so the hint names the right program:
@@ -202,7 +202,7 @@ An application binary also explains the most common mistake: running it from the
cd ../..; bin/acme migrate
```
Only the message changes: the binary still reads its configuration from `config/` in the working directory. `summer` is not affected, because its commands find the application root themselves.
The suggested command repeats your arguments, except that the value of a flag whose name contains `password`, `passwd`, `secret`, `token` or `key` is masked as `***` (`--password ***`, `--api-token=***`), so a secret typed on the command line does not reach logs that capture stderr. Only the message changes: the binary still reads its configuration from `config/` in the working directory. `summer` is not affected, because its commands find the application root themselves.
## Documentation coverage

View File

@@ -97,7 +97,7 @@ Every record carries the attribute `component=blog`, and passes the component's
A daily channel with `path: storage/logs/summer.log` writes `storage/logs/summer-2026-10-21.log` today and `storage/logs/summer-2026-10-22.log` after local midnight. A relative path resolves against the application root, the directory that holds `config/`, and may not leave it; an absolute path is used as given. Files are created with mode 0640 in directories created with mode 0750, because logs carry user identifiers, and a file is only created once a record reaches it. With `days: 14` the files of today and the 13 days before are kept and older files of the same name pattern are deleted; other files in the directory are never touched. `days: 0` keeps every file.
If the directory cannot be created or the file cannot be opened (a read-only container filesystem, missing permissions), the channel logs one warning from component `diary` on the console naming the path and the error, and is disabled. The start-up never fails because of a log file.
If the directory cannot be created or the file cannot be opened (a read-only container filesystem, missing permissions), the channel logs a warning from component `diary` on the console naming the path and the error, once per day. It then skips records and tries to open the file again one minute later, or at the first record of the next day, whichever comes first, so a passing failure (a full disk, too many open files, a directory removed for a moment during rotation) does not end file logging until the next restart. The start-up never fails because of a log file.
## Redaction

View File

@@ -43,6 +43,7 @@ Applications pin exact versions, so a new release never reaches an application u
- **Exit codes.** A failed command exits 1 and a usage error (an unknown command or flag, a wrong number of arguments) exits 2. Errors print as a block on stderr with a hint where the framework knows the fix, and `-v` shows the wrapped errors.
- **`-q` keeps data.** `-q` hides information and success lines, spinners and progress bars, but still prints what a command exists to produce: the token of `admin:token:create`, the key of `key:generate` and every `--json` output.
- **Production confirmation.** `migrate`, `migrate:rollback`, `deferred:purge` and `queue:clear` ask before they run when the environment is `production` and they run on an interactive terminal; `--force` skips the question. Off a terminal, as in deploy scripts over SSH, nothing changes.
- **`summer` delegates keep the binary's exit code.** Since 0.4.2 `summer migrate`, `summer serve` and the other delegated commands exit with the app binary's exit code and print only the binary's error block; 0.4.0 and 0.4.1 printed a second block and exited 1. The binary also gets the terminal again, so the production question, styled summaries and spinners work through `summer`, and its stderr no longer lands in `summer`'s stdout: a script that captured `summer route:list --json` together with log lines now gets only the JSON.
- **`summer dev` runs `serve`.** It used to start the binary without a command, which printed the help and stopped; it now runs `bin/<app> serve` with the global options you give `summer dev`.
- **Result lines.** `summer build` ends with `Built bin/<app> in <time>`, the `make:` commands print `Created <path>` per file, `migrate` prints `Applied N migrations` or `Nothing to migrate`, and `queue:clear` prints `Cleared N jobs from <queue>`. Adjust scripts that matched the old lower-case lines.

View File

@@ -19,6 +19,13 @@ import (
const defaultDebounce = 200 * time.Millisecond
// ShutdownGrace is how long a stopped app binary may take to exit after
// SIGTERM before it is killed, for summer dev and summer's delegates alike.
// It is longer than the 10-second shutdown timeout of serve (its request
// drain) and the workers, so they finish a graceful shutdown and print
// their report.
const ShutdownGrace = 15 * time.Second
// Options customizes the watch loop. Tests inject Build and Start.
type Options struct {
Build func(ctx context.Context, appDir string, out io.Writer) error
@@ -224,6 +231,8 @@ func startChild(ctx context.Context, opts Options, binPath string, args []string
return &child{cmd: cmd, wait: ch}, nil
}
// stop sends the child SIGTERM and waits for it to exit, killing it only
// after ShutdownGrace.
func (c *child) stop() {
if c == nil || c.cmd == nil || c.cmd.Process == nil {
return
@@ -231,7 +240,7 @@ func (c *child) stop() {
_ = c.cmd.Process.Signal(syscall.SIGTERM)
select {
case <-c.wait:
case <-time.After(2 * time.Second):
case <-time.After(ShutdownGrace):
_ = c.cmd.Process.Kill()
<-c.wait
}

View File

@@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
@@ -472,3 +473,191 @@ func waitUntil(t *testing.T, name string, ok func() bool) {
}
t.Fatalf("timed out waiting for %s", name)
}
// fakeServe writes an executable that records its arguments in
// $DEV_ARGS_FILE and, on SIGTERM, writes "term" to $DEV_TERM_FILE and exits
// 0, like serve's graceful stop.
func fakeServe(t *testing.T, path string) (argsFile, termFile string) {
t.Helper()
dir := t.TempDir()
argsFile, termFile = filepath.Join(dir, "args"), filepath.Join(dir, "term")
t.Setenv("DEV_ARGS_FILE", argsFile)
t.Setenv("DEV_TERM_FILE", termFile)
script := "#!/bin/sh\n" +
"printf '%s\\n' \"$*\" > \"$DEV_ARGS_FILE\"\n" +
"trap 'echo term > \"$DEV_TERM_FILE\"; exit 0' TERM\n" +
"while :; do sleep 0.05; done\n"
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
return argsFile, termFile
}
func readTrimmed(path string) string {
b, _ := os.ReadFile(path)
return strings.TrimSpace(string(b))
}
// TestStartBinServe: summer dev starts bin/<binary> with "serve" first and
// then the forwarded global options, through the real startBin.
func TestStartBinServe(t *testing.T) {
bin := filepath.Join(t.TempDir(), "bin", "hello")
argsFile, _ := fakeServe(t, bin)
var out safeBuffer
cmd, err := startBin(t.Context(), bin, []string{"serve", "-vv", "--env=staging"}, &out, &out)
if err != nil {
t.Fatal(err)
}
waitUntil(t, "args written", func() bool { return readTrimmed(argsFile) != "" })
_ = cmd.Process.Signal(syscall.SIGTERM)
_ = cmd.Wait()
if got := readTrimmed(argsFile); got != "serve -vv --env=staging" {
t.Fatalf("child args = %q", got)
}
// Through watch: the default Start runs serve with the globals summer
// dev was given.
dir := t.TempDir()
writeWatchApp(t, dir)
argsFile, _ = fakeServe(t, filepath.Join(dir, "bin", "hello"))
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
done := make(chan error, 1)
opts := Options{Debounce: 40 * time.Millisecond, Build: func(context.Context, string, io.Writer) error { return nil }}
go func() { done <- watch(ctx, dir, testOutput(&out), []string{"-q", "--no-ansi"}, opts) }()
waitUntil(t, "child started", func() bool { return readTrimmed(argsFile) != "" })
if got := readTrimmed(argsFile); got != "serve -q --no-ansi" {
t.Fatalf("watch started the child with %q", got)
}
if !strings.Contains(out.String(), "running bin/hello serve -q --no-ansi") {
t.Errorf("watch line lacks the child command:\n%s", out.String())
}
cancel()
<-done
}
// TestDevReport: the dev report counts successful and failed rebuilds and
// restarts; a failed build keeps the running child.
func TestDevReport(t *testing.T) {
r := devReport{rebuilds: 2, failed: 1, restarts: 1, started: time.Now().Add(-90 * time.Second)}
d := r.details()
if d[0].Value != "3 (1 failed)" || !d[0].Warn || d[1].Value != "1" || d[2].Value != "1m30s" {
t.Fatalf("details = %+v", d)
}
if clean := (devReport{rebuilds: 1, started: time.Now()}).details(); clean[0].Value != "1 (0 failed)" || clean[0].Warn {
t.Fatalf("clean details = %+v", clean)
}
dir := t.TempDir()
writeWatchApp(t, dir)
var out safeBuffer
var builds, starts atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(context.Context, string, io.Writer) error {
if builds.Add(1) == 2 {
return fmt.Errorf("syntax error")
}
return nil
},
Start: func(context.Context, string, []string, io.Writer, io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
return cmd, cmd.Start()
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
write(t, filepath.Join(dir, "plugin.go"), "package p\n// broken\n")
waitUntil(t, "failed build", func() bool { return builds.Load() == 2 })
waitUntil(t, "build failure line", func() bool { return strings.Contains(out.String(), "Build failed: syntax error") })
write(t, filepath.Join(dir, "plugin.go"), "package p\n// fixed\n")
waitUntil(t, "restart", func() bool { return starts.Load() == 2 })
cancel()
if err := <-done; err != nil {
t.Fatalf("watch returned %v", err)
}
got := out.String()
for _, want := range []string{"Dev report", "Rebuilds 3 (1 failed)", "Restarts 1", "Uptime"} {
if !strings.Contains(got, want) {
t.Errorf("dev report lacks %q:\n%s", want, got)
}
}
}
// TestDevGracefulNil: when summer dev is stopped it sends SIGTERM to the
// running serve, which stops gracefully, prints the dev report and returns
// nil (exit 0).
func TestDevGracefulNil(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
argsFile, termFile := fakeServe(t, filepath.Join(dir, "bin", "hello"))
var out safeBuffer
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
done := make(chan error, 1)
opts := Options{Debounce: 40 * time.Millisecond, Build: func(context.Context, string, io.Writer) error { return nil }}
go func() { done <- watch(ctx, dir, testOutput(&out), nil, opts) }()
waitUntil(t, "child started", func() bool { return readTrimmed(argsFile) == "serve" })
cancel()
select {
case err := <-done:
if err != nil {
t.Fatalf("watch returned %v after stop, want nil", err)
}
case <-time.After(5 * time.Second):
t.Fatal("watch did not stop")
}
if readTrimmed(termFile) != "term" {
t.Fatal("serve did not get SIGTERM")
}
if !strings.Contains(out.String(), "Dev report") || !strings.Contains(out.String(), "Restarts 0") {
t.Fatalf("no dev report:\n%s", out.String())
}
}
// TestStopHelperProcess is not a real test: TestDevStopWaitsForGracefulShutdown
// re-executes the test binary with DEV_STOP_HELPER=1 to get a child that,
// like serve draining requests, takes 2.5 seconds to shut down after
// SIGTERM and then exits 0.
func TestStopHelperProcess(t *testing.T) {
if os.Getenv("DEV_STOP_HELPER") != "1" {
return
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
fmt.Println("ready")
select {
case <-sigs:
time.Sleep(2500 * time.Millisecond)
os.Exit(0)
case <-time.After(30 * time.Second):
os.Exit(3)
}
}
// TestDevStopWaitsForGracefulShutdown (WR-03): child.stop sends SIGTERM
// and waits for a child that takes longer than two seconds to drain
// instead of killing it; the grace exceeds serve's 10-second drain.
func TestDevStopWaitsForGracefulShutdown(t *testing.T) {
if ShutdownGrace <= 10*time.Second {
t.Fatalf("ShutdownGrace %s does not exceed serve's 10-second drain", ShutdownGrace)
}
t.Setenv("DEV_STOP_HELPER", "1")
var out safeBuffer
c, err := startChild(t.Context(), Options{Start: startBin}, os.Args[0], []string{"-test.run=^TestStopHelperProcess$"}, &out, &out)
if err != nil {
t.Fatal(err)
}
waitUntil(t, "helper ready", func() bool { return strings.Contains(out.String(), "ready") })
c.stop()
if st := c.cmd.ProcessState; st == nil || !st.Exited() || st.ExitCode() != 0 {
t.Fatalf("child was not allowed to finish its shutdown: %v", st)
}
}

View File

@@ -0,0 +1,43 @@
package docsite
import "testing"
// TestCommandWordGlobals: the docs checker finds the command word behind
// the console's global options (D-13), in every form a page may use them.
func TestCommandWordGlobals(t *testing.T) {
tool, app := true, false
cases := []struct {
in string
name string
tool bool
}{
{"bin/acme -v serve", "serve", app},
{"bin/acme -vv serve", "serve", app},
{"./bin/acme -vvv serve --port 8080", "serve", app},
{"summer -q migrate", "migrate", tool},
{"summer --quiet migrate", "migrate", tool},
{"summer -qn migrate", "migrate", tool},
{"bin/acme --env staging migrate", "migrate", app},
{"bin/acme --env=staging migrate", "migrate", app},
{"bin/acme --no-ansi --env=staging -n migrate --force", "migrate", app},
{"bin/acme --ansi -v queue:work", "queue:work", app},
{"summer list", "list", tool},
{"summer -v list --format=json", "list", tool},
{"bin/acme help migrate", "help", app},
{"bin/acme -n help migrate", "help", app},
{"$ SUMMER_ENV=dev bin/acme --env dev -q migrate:status", "migrate:status", app},
{"go run ./cmd/summer -v docs:build --check", "docs:build", tool},
}
for _, tc := range cases {
name, isTool, ok := commandWord(tc.in)
if !ok || name != tc.name || isTool != tc.tool {
t.Errorf("commandWord(%q) = %q, %v, %v; want %q, %v, true", tc.in, name, isTool, ok, tc.name, tc.tool)
}
}
// Only global options and no command word.
for _, in := range []string{"summer -v", "bin/acme --env staging", "summer -q --no-ansi -n", "bin/acme --env"} {
if name, _, ok := commandWord(in); ok {
t.Errorf("commandWord(%q) found %q, want no command", in, name)
}
}
}

View File

@@ -27,7 +27,7 @@ Use cases:
- Usage strings and argument-count checks derived from the declared arguments (`<name>` for required, `[name]` for optional).
- Scalar flags, bare flags (`bonfire.Flag.Bare`, so `--force` alone stores `true`) and ordered repeatable flags (`bonfire.Flag.Repeatable`, read back through `bonfire.Input.Flags`).
- Styled status lines: `bonfire.Output.Info`, `bonfire.Output.Success`, `bonfire.Output.Warning` and `bonfire.Output.Error` (the last one writes to the error stream).
- Widgets on the charm stack, ported from SSU's terminal UI: box-drawn tables (`bonfire.Output.Table`, lipgloss), a spinner around a function (`bonfire.Output.Spinner`), a gradient progress bar (`bonfire.Output.Progress`, whose `bonfire.Progress` also implements `bonfire.ItemProgress`) and a multi-row process view for bulk loops (`bonfire.Process`), each a bubbletea program that keeps stdin and signals to the command. Log lines and output printed while a view runs appear above it (`bonfire.PrintAbove`).
- Widgets on the charm stack, ported from SSU's terminal UI: box-drawn tables (`bonfire.Output.Table`, lipgloss), a spinner around a function (`bonfire.Output.Spinner`), a gradient progress bar (`bonfire.Output.Progress`, whose `bonfire.Progress` also implements `bonfire.ItemProgress`) and a multi-row process view for bulk loops (`bonfire.Process`), each a bubbletea program that keeps stdin and signals to the command: Ctrl+C or SIGTERM during a view restores the cursor and the terminal, then stops the command as before (or reaches its own signal handler). Log lines, and output the command prints through the console that started the view, appear above a running view (`bonfire.PrintAbove`); other consoles (a `bonfire.Call` buffer, a console on a file) keep writing to their own writers. Table cells, process-view item names and errors, and failure lines are stripped of control characters (`bonfire.Sanitize`).
- Prompts: `bonfire.Output.Ask`, `bonfire.Output.Confirm` and `bonfire.Output.Secret` use huh fields on an interactive terminal, and `bonfire.Output.Choice` an arrow-key selector with a confirmation screen. Off a terminal they read lines from stdin as before (a piped password still works), return their defaults when input ends, and `bonfire.Output.Confirm` returns its default without asking. A cancelled prompt returns `bonfire.ErrAborted`. `bonfire.ConfirmProceed` asks before a destructive step only on an interactive terminal.
- Degradation: every widget follows the terminal and the global options, and the `bonfire.Output` interface is unchanged for plugins:
@@ -46,7 +46,7 @@ Use cases:
- Hidden commands (`bonfire.Command.Hidden`) run but stay out of the list unless `list --all`.
- JSON catalog: `list --format=json` prints every command, hidden ones flagged, with arguments, options, defaults and examples as a `bonfire.Listing` (`bonfire.Describe` builds it from a command slice).
- Documentation coverage: `bonfire.CheckDocs` reports commands lacking a description, long help, examples, or argument and option descriptions.
- Error block and exit codes: `bonfire.Execute` runs the root through fang and returns `bonfire.ExitOK`, `bonfire.ExitFailure`, or `bonfire.ExitUsage` for an unknown command or flag and a wrong number of arguments (`bonfire.UsageError`). Errors print on the error stream as a block (`bonfire.RenderError`): the ERROR badge and the message, the explanation and Try lines of `bonfire.WithHint` (`{bin}` becomes `bin/<name>` or `summer`), cobra's "did you mean" suggestions as Try lines, and "Run with -v for details." or, at `-v`, the wrapped error chain; every message passes through `sunscreen.Scrub`. Errors wrapped by `bonfire.Reported` print nothing. `bonfire.Fail` prints errors that happen before the root exists. For an application binary (any root other than `summer`) that fails, other than with a usage error, while the working directory has no `config/` directory and a parent directory holds `summer.yaml`, both add the hint "This command runs from the application root." with `cd <relative path>; bin/<name> <args>` (only the message changes).
- Error block and exit codes: `bonfire.Execute` runs the root through fang and returns `bonfire.ExitOK`, `bonfire.ExitFailure`, or `bonfire.ExitUsage` for an unknown command or flag and a wrong number of arguments (`bonfire.UsageError`). Errors print on the error stream as a block (`bonfire.RenderError`): the ERROR badge and the message, the explanation and Try lines of `bonfire.WithHint` (`{bin}` becomes `bin/<name>` or `summer`), cobra's "did you mean" suggestions as Try lines, and "Run with -v for details." or, at `-v`, the wrapped error chain; every message passes through `sunscreen.Scrub` and every line through `bonfire.Sanitize`, which strips control characters. Errors wrapped by `bonfire.Reported` print nothing. An error carrying a code through `bonfire.WithExitCode` exits with that code; summer uses it to exit with the code of an app binary it ran. `bonfire.Fail` prints errors that happen before the root exists. For an application binary (any root other than `summer`) that fails, other than with a usage error, while the working directory has no `config/` directory and a parent directory holds `summer.yaml`, both add the hint "This command runs from the application root." with `cd <relative path>; bin/<name> <args>` (only the message changes); the values of flags named like a password, secret, token or key are masked as `***` in that line.
- Name checks at boot: two commands with the same name, or a command named `list` or `help`, make `bonfire.NewRoot` fail with `bonfire.ErrDuplicateCommand`; a command flag that reuses a global option's name or shorthand fails with `bonfire.ErrReservedFlag` instead of panicking.
- Injectable streams (`bonfire.NewRootIO`, `bonfire.NewOutput`) so commands can be tested against buffers.
- In-process calls: `bonfire.Call` runs a named command with arguments against any writer (Laravel `Artisan::call`), and `bonfire.Catalog` holds an application binary's final command list so code outside the Cobra root, such as the conga scheduler, can call any registered command.
@@ -139,7 +139,9 @@ if catalog.Has("blog:import") {
| `bonfire.HintError` | An error with an `Explanation` and a `Try` text that the error block prints. |
| `bonfire.WithHint` | Wraps an error in a `bonfire.HintError`; `{bin}` in the Try text becomes `bin/<name>` or `summer`. |
| `bonfire.ErrReported` | Marks an error whose details the command already printed. |
| `bonfire.Reported` | Wraps an error so it matches `bonfire.ErrReported`: exit code 1, no error block. |
| `bonfire.Reported` | Wraps an error so it matches `bonfire.ErrReported`: exit code 1, or the code of `bonfire.WithExitCode`, and no error block. |
| `bonfire.Sanitize` | Removes control characters other than tab (line breaks become spaces), so untrusted text cannot move the cursor or retitle the terminal; error blocks, process views, failure lines, tables and diary's console use it. |
| `bonfire.WithExitCode` | Makes an error carry the exit code `bonfire.Execute` returns for it (1 to 255; other codes give 1). |
| `bonfire.Listing` | The JSON catalog of `list --format=json`: `Application` (`bonfire.ApplicationInfo`), `GlobalOptions` and `Commands`. |
| `bonfire.ApplicationInfo` | Name, version and framework version of a binary in a `bonfire.Listing`. |
| `bonfire.CommandInfo` | One command of a `bonfire.Listing`: name, description, long help, usage, hidden and passthrough switches, `Arguments`, `Options` and `Examples`. |

View File

@@ -0,0 +1,50 @@
package bonfire
import (
"reflect"
"testing"
)
// TestCheckDocs: every missing item is reported in a fixed order, blank
// text counts as missing, results are sorted by command name and a fully
// documented command reports nothing.
func TestCheckDocs(t *testing.T) {
complete := Command{
Name: "acme:sync", Description: "Sync", Long: "Syncs.",
Args: []Arg{{Name: "target", Description: "Where to"}},
Flags: []Flag{{Name: "dry-run", Description: "Only print", Bare: true}},
Examples: []Example{{Command: "acme:sync prod"}},
}
if got := CheckDocs([]Command{complete}); len(got) != 0 {
t.Fatalf("complete command reported: %v", got)
}
if got := CheckDocs(nil); len(got) != 0 {
t.Fatalf("no commands reported: %v", got)
}
blank := complete
blank.Name, blank.Description, blank.Long = "acme:blank", " ", "\n\t"
noExamples := complete
noExamples.Name, noExamples.Examples = "acme:noex", nil
undocumented := Command{
Name: "acme:bare",
Args: []Arg{{Name: "a"}, {Name: "b", Description: "B"}},
Flags: []Flag{{Name: "x"}, {Name: "y", Description: "Y"}, {Name: "z", Description: " "}},
}
got := CheckDocs([]Command{noExamples, complete, undocumented, blank})
want := []DocProblem{
{Command: "acme:bare", Missing: []string{"description", "long help", "examples", "argument a description", "option --x description", "option --z description"}},
{Command: "acme:blank", Missing: []string{"description", "long help"}},
{Command: "acme:noex", Missing: []string{"examples"}},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("CheckDocs =\n %v\nwant\n %v", got, want)
}
if s := got[1].String(); s != "acme:blank: missing description, long help" {
t.Errorf("String() = %q", s)
}
// The builtins list and help are documented.
if got := CheckDocs(builtinCommands()); len(got) != 0 {
t.Errorf("builtin commands reported: %v", got)
}
}

View File

@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"unicode"
"git.golem15.com/golem15/summercms/modules/sunscreen"
"github.com/charmbracelet/colorprofile"
@@ -93,13 +94,77 @@ func Reported(err error) error {
return &reportedError{err: err}
}
// Sanitize removes control characters other than tab from s; carriage
// returns and line feeds become spaces, so a multi-line value stays on one
// line. Text from outside the operator's control (database or remote error
// text, names an administrator typed) then cannot move the cursor, clear
// the screen or set the window title. Error blocks, process views, failure
// lines and tables pass their text through it, and so does diary's console
// channel. A clean string is returned unchanged.
func Sanitize(s string) string {
clean := true
for _, r := range s {
if r != '\t' && unicode.IsControl(r) {
clean = false
break
}
}
if clean {
return s
}
var b strings.Builder
for _, r := range s {
switch {
case r == '\n' || r == '\r':
b.WriteByte(' ')
case r != '\t' && unicode.IsControl(r):
default:
b.WriteRune(r)
}
}
return b.String()
}
// sanitizeLines applies Sanitize to every line of s, keeping the line
// breaks.
func sanitizeLines(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
lines[i] = Sanitize(line)
}
return strings.Join(lines, "\n")
}
// exitCodeError carries the exit code Execute returns for an error.
type exitCodeError struct {
err error
code int
}
func (e *exitCodeError) Error() string { return e.err.Error() }
func (e *exitCodeError) Unwrap() error { return e.err }
// WithExitCode returns err carrying code as the process exit code: Execute
// returns code instead of ExitFailure or ExitUsage. A code outside 1 to 255
// gives ExitFailure. A nil err stays nil. Wrap it with Reported when the
// error was already printed, as summer does with the exit code of an app
// binary it ran.
func WithExitCode(err error, code int) error {
if err == nil {
return nil
}
return &exitCodeError{err: err, code: code}
}
// RenderError writes err to w as the console error block: the ERROR badge
// and the message, the explanation and the Try lines of a HintError ({bin}
// replaced by summer or bin/<name>, for the root named name), then "Run with
// -v for details." at verbosity 0 or the wrapped error chain under "Caused
// by:" at -v and above. cobra's unknown command error becomes `Command "x"
// is not defined.` with its suggestions as Try lines, and other usage errors
// suggest the command's help. Every message passes through sunscreen.Scrub.
// suggest the command's help. Every message passes through sunscreen.Scrub,
// and every line through Sanitize.
// Errors wrapped by Reported print nothing.
func RenderError(w io.Writer, name string, err error, g Globals) {
if err == nil || w == nil || errors.Is(err, ErrReported) {
@@ -142,7 +207,7 @@ func RenderError(w io.Writer, name string, err error, g Globals) {
var b strings.Builder
b.WriteString("\n")
for i, line := range strings.Split(sunscreen.Scrub(strings.TrimRight(message, "\n")), "\n") {
for i, line := range strings.Split(sanitizeLines(sunscreen.Scrub(strings.TrimRight(message, "\n"))), "\n") {
if i == 0 {
b.WriteString(" " + p.ErrorBold.Reverse(true).Render(" ERROR ") + " " + line + "\n")
continue
@@ -151,21 +216,21 @@ func RenderError(w io.Writer, name string, err error, g Globals) {
}
if explanation != "" {
b.WriteString("\n")
for _, line := range strings.Split(sunscreen.Scrub(explanation), "\n") {
for _, line := range strings.Split(sanitizeLines(sunscreen.Scrub(explanation)), "\n") {
b.WriteString(" " + line + "\n")
}
}
if try != "" {
b.WriteString("\n " + p.Header.Render("Try:") + "\n")
for _, line := range strings.Split(strings.ReplaceAll(try, "{bin}", bin), "\n") {
b.WriteString(" " + p.Success.Render(sunscreen.Scrub(line)) + "\n")
b.WriteString(" " + p.Success.Render(Sanitize(sunscreen.Scrub(line))) + "\n")
}
}
if g.Verbosity > 0 {
if chain := causes(err, err.Error()); len(chain) > 0 {
b.WriteString("\n " + p.Header.Render("Caused by:") + "\n")
for _, c := range chain {
b.WriteString(" " + p.Muted.Render("- "+sunscreen.Scrub(c)) + "\n")
b.WriteString(" " + p.Muted.Render("- "+sanitizeLines(sunscreen.Scrub(c))) + "\n")
}
}
} else {
@@ -183,7 +248,8 @@ const wrongDirectoryExplanation = "This command runs from the application root."
// name other than summer) failed while the working directory dir ("" means
// os.Getwd) has no config directory and a parent directory holds
// summer.yaml: compass then found no configuration, which is why the command
// failed (D-19). The Try line is "cd <relative path>; bin/<name> <args>".
// failed (D-19). The Try line is "cd <relative path>; bin/<name> <args>",
// with the values of secret-looking flags masked (tryArgs).
// Usage errors, Reported errors and summer are left alone; nothing else
// about the failure changes.
func wrongDirectory(name string, err error, args []string, dir string) error {
@@ -217,13 +283,54 @@ func wrongDirectory(name string, err error, args []string, dir string) error {
if rerr != nil {
return err
}
try := "cd " + shellQuote(rel) + "; {bin}"
for _, a := range args {
try += " " + shellQuote(a)
}
try := "cd " + shellQuote(rel) + "; {bin}" + tryArgs(args)
return &HintError{Err: err, Explanation: wrongDirectoryExplanation, Try: try}
}
// secretWords mark a long flag whose value the Try line masks.
var secretWords = []string{"password", "passwd", "secret", "token", "key"}
// secretFlag reports whether the long flag name (without "--") looks like it
// carries a secret.
func secretFlag(name string) bool {
name = strings.ToLower(name)
for _, w := range secretWords {
if strings.Contains(name, w) {
return true
}
}
return false
}
// tryArgs renders args for the wrong-directory Try line, each preceded by a
// space and quoted for a shell. The value of a long flag named like a
// secret becomes ***, in the "--flag=value" form and in the "--flag value"
// form (the next argument, when it does not start with "-"), so a password
// typed on the command line does not reach stderr and CI logs.
func tryArgs(args []string) string {
var b strings.Builder
for i := 0; i < len(args); i++ {
a := args[i]
b.WriteByte(' ')
name, long := strings.CutPrefix(a, "--")
flag, _, inline := strings.Cut(name, "=")
if !long || flag == "" || !secretFlag(flag) {
b.WriteString(shellQuote(a))
continue
}
if inline {
b.WriteString(shellQuote("--"+flag) + "=***")
continue
}
b.WriteString(shellQuote(a))
if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
b.WriteString(" ***")
i++
}
}
return b.String()
}
// shellQuote single-quotes s when a shell would split or expand it.
func shellQuote(s string) string {
if s != "" && !strings.ContainsAny(s, " \t\n'\"\\$`*?[]{}()<>|&;#~!") {

View File

@@ -0,0 +1,325 @@
package bonfire
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// TestExitCodes: Execute returns 0 on success, 1 on a failure and 2 on an
// unknown command, an unknown flag, a wrong argument count and an unknown
// help topic; Fail returns 0 for nil and 1 otherwise.
func TestExitCodes(t *testing.T) {
plainEnv(t)
commands := []Command{
{Name: "acme:ok", Description: "Succeed", Run: func(context.Context, Input, Output) error { return nil }},
{Name: "acme:fail", Description: "Fail", Run: func(context.Context, Input, Output) error { return errors.New("boom") }},
{Name: "acme:usage", Description: "Wrapped usage", Run: func(context.Context, Input, Output) error {
return fmt.Errorf("acme:usage: %w", Usage(errors.New("pick one of a, b")))
}},
{Name: "acme:one", Description: "Exactly one", Args: []Arg{{Name: "x", Required: true}}, Run: func(context.Context, Input, Output) error { return nil }},
{Name: "acme:min", Description: "At least one", Args: []Arg{{Name: "x", Required: true}, {Name: "y"}}, Run: func(context.Context, Input, Output) error { return nil }},
}
cases := []struct {
args []string
want int
}{
{nil, ExitOK},
{[]string{"acme:ok"}, ExitOK},
{[]string{"-q", "acme:ok", "-n"}, ExitOK},
{[]string{"acme:one", "a"}, ExitOK},
{[]string{"acme:min", "a", "b"}, ExitOK},
{[]string{"acme:fail"}, ExitFailure},
{[]string{"acme:usage"}, ExitUsage},
{[]string{"acme:nope"}, ExitUsage},
{[]string{"-v", "acme:nope"}, ExitUsage},
{[]string{"acme:ok", "--bogus"}, ExitUsage},
{[]string{"--bogus", "acme:ok"}, ExitUsage},
{[]string{"acme:one"}, ExitUsage},
{[]string{"acme:one", "a", "b"}, ExitUsage},
{[]string{"acme:min"}, ExitUsage},
{[]string{"help", "acme:nope"}, ExitUsage},
{[]string{"list", "--format=yaml"}, ExitUsage},
}
for _, tc := range cases {
_, errOut, code := runGolden(t, commands, tc.args...)
if code != tc.want {
t.Errorf("%q: exit %d, want %d\n%s", tc.args, code, tc.want, errOut)
}
if tc.want != ExitOK && !strings.Contains(errOut, "ERROR") {
t.Errorf("%q: no error block on stderr: %q", tc.args, errOut)
}
if tc.want == ExitUsage && !strings.Contains(errOut, "Try:") {
t.Errorf("%q: usage error without a Try line:\n%s", tc.args, errOut)
}
}
var buf bytes.Buffer
if code := Fail(&buf, "acme", nil, Globals{}); code != ExitOK || buf.Len() != 0 {
t.Errorf("Fail(nil) = %d, %q", code, buf.String())
}
if code := Fail(&buf, "acme", errors.New("config/app.yaml: bad"), Globals{}); code != ExitFailure || !strings.Contains(buf.String(), "config/app.yaml: bad") {
t.Errorf("Fail(err) = %d, %q", code, buf.String())
}
}
// TestReportedSilent: a Reported error, also wrapped further, prints no
// error block and exits 1; it matches ErrReported and the original error.
func TestReportedSilent(t *testing.T) {
plainEnv(t)
base := errors.New("3 problems")
rep := Reported(base)
if !errors.Is(rep, ErrReported) || !errors.Is(rep, base) || rep.Error() != "3 problems" {
t.Fatalf("Reported(base) = %v", rep)
}
if !errors.Is(Reported(nil), ErrReported) {
t.Fatal("Reported(nil) is not ErrReported")
}
var buf bytes.Buffer
for _, err := range []error{rep, fmt.Errorf("docs:build: %w", rep), Reported(nil)} {
RenderError(&buf, "acme", err, Globals{Verbosity: 3})
}
if buf.Len() != 0 {
t.Fatalf("RenderError printed a reported error: %q", buf.String())
}
cmd := Command{Name: "docs:check", Description: "Check", Run: func(_ context.Context, _ Input, out Output) error {
out.Println("page.md: broken link")
return fmt.Errorf("docs:check: %w", Reported(errors.New("1 problem")))
}}
out, errOut, code := runGolden(t, []Command{cmd}, "-v", "docs:check")
if code != ExitFailure || errOut != "" || out != "page.md: broken link\n" {
t.Fatalf("exit %d, stdout %q, stderr %q", code, out, errOut)
}
}
// TestErrorScrubbed: credential shapes in the message, the explanation,
// the Try line and the -v chain are scrubbed by sunscreen.
func TestErrorScrubbed(t *testing.T) {
const bearer = "abcdefghijklmnop0123456789.payload.sig"
const skKey = "sk-ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
const apiKey = "live-key-0123456789"
inner := fmt.Errorf("upstream answered 401 for x-api-key: %s", apiKey)
err := WithHint(fmt.Errorf("calling the API with Bearer %s: %w", bearer, inner),
"The configured key "+skKey+" was refused.",
"{bin} acme:login --token Bearer "+bearer)
for _, v := range []int{0, 1} {
var buf bytes.Buffer
RenderError(&buf, "acme", err, Globals{Verbosity: v, Color: ColorNever})
out := buf.String()
for _, secret := range []string{bearer, skKey, apiKey} {
if strings.Contains(out, secret) {
t.Errorf("verbosity %d leaks %q:\n%s", v, secret, out)
}
}
for _, want := range []string{"Bearer [REDACTED]", "sk-[REDACTED]", "bin/acme acme:login"} {
if !strings.Contains(out, want) {
t.Errorf("verbosity %d lacks %q:\n%s", v, want, out)
}
}
if v == 1 && !strings.Contains(out, "x-api-key: [REDACTED]") {
t.Errorf("the -v chain is not scrubbed:\n%s", out)
}
}
}
// TestWrongDirectoryHint: an application binary that fails in a directory
// without config/ below an application root (summer.yaml) gets the
// "cd <rel>; bin/<name> <args>" hint; summer, the root itself, usage
// errors and directories outside an application get none.
func TestWrongDirectoryHint(t *testing.T) {
plainEnv(t)
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "summer.yaml"), []byte("binary: acme\n"), 0o644); err != nil {
t.Fatal(err)
}
for _, dir := range []string{"config", "plugins/acme/blog"} {
if err := os.MkdirAll(filepath.Join(root, dir), 0o755); err != nil {
t.Fatal(err)
}
}
migrate := []Command{{Name: "migrate", Description: "Run the migrations", Run: func(context.Context, Input, Output) error {
return errors.New("lagoon: database.dsn is empty")
}}}
run := func(t *testing.T, name, dir string, args ...string) string {
t.Helper()
t.Chdir(dir)
var out, errW bytes.Buffer
r, err := NewRootIO(name, migrate, strings.NewReader(""), &out, &errW, WithVersion(goldenVersion))
if err != nil {
t.Fatal(err)
}
if code := Execute(context.Background(), r, args); code != ExitFailure && code != ExitUsage {
t.Fatalf("exit %d", code)
}
return errW.String()
}
got := run(t, "acme", filepath.Join(root, "plugins"), "migrate")
if !strings.Contains(got, wrongDirectoryExplanation) || !strings.Contains(got, "Try:\n cd ..; bin/acme migrate\n") {
t.Errorf("plugins/: no hint:\n%s", got)
}
got = run(t, "acme", filepath.Join(root, "plugins", "acme", "blog"), "-v", "migrate", "--env=staging")
if !strings.Contains(got, "cd ../../..; bin/acme -v migrate --env=staging") {
t.Errorf("plugins/acme/blog: hint lacks the path or the args:\n%s", got)
}
for name, tc := range map[string]struct {
bin, dir string
args []string
}{
"summer": {"summer", filepath.Join(root, "plugins"), []string{"migrate"}},
"app root": {"acme", root, []string{"migrate"}},
"usage error": {"acme", filepath.Join(root, "plugins"), []string{"migrate", "--bogus"}},
"no application": {"acme", t.TempDir(), []string{"migrate"}},
} {
if got := run(t, tc.bin, tc.dir, tc.args...); strings.Contains(got, wrongDirectoryExplanation) {
t.Errorf("%s: unexpected hint:\n%s", name, got)
}
}
}
// TestWithExitCode (CR-02): Execute returns the code an error carries
// through WithExitCode, prints no block when it is also Reported, maps a
// code outside 1..255 to ExitFailure, and errors.Is and errors.As still see
// the wrapped error.
func TestWithExitCode(t *testing.T) {
plainEnv(t)
base := errors.New("child failed")
command := func(err error) []Command {
return []Command{{Name: "acme:run", Description: "Run", Run: func(context.Context, Input, Output) error { return err }}}
}
_, errOut, code := runGolden(t, command(Reported(WithExitCode(base, 2))), "acme:run")
if code != 2 || errOut != "" {
t.Errorf("Reported(WithExitCode(err, 2)): exit %d, stderr %q; want 2 and no block", code, errOut)
}
_, errOut, code = runGolden(t, command(WithExitCode(base, 3)), "acme:run")
if code != 3 || !strings.Contains(errOut, "ERROR") || !strings.Contains(errOut, "child failed") {
t.Errorf("WithExitCode(err, 3): exit %d, stderr %q; want 3 and the block", code, errOut)
}
for _, bad := range []int{0, -1, 256} {
if _, _, code := runGolden(t, command(Reported(WithExitCode(base, bad))), "acme:run"); code != ExitFailure {
t.Errorf("WithExitCode(err, %d): exit %d, want %d", bad, code, ExitFailure)
}
}
if WithExitCode(nil, 2) != nil {
t.Error("WithExitCode(nil, 2) is not nil")
}
wrapped := fmt.Errorf("outer: %w", WithExitCode(&HintError{Err: base, Try: "x"}, 4))
var hint *HintError
if !errors.Is(wrapped, base) || !errors.As(wrapped, &hint) || wrapped.Error() != "outer: child failed" {
t.Errorf("WithExitCode hides the wrapped error: %v", wrapped)
}
if code := exitCode(wrapped); code != 4 {
t.Errorf("exitCode through a wrapper = %d, want 4", code)
}
}
// TestWrongDirectoryMasksSecrets (WR-08): the wrong-directory Try line
// replaces the value of every flag named like a password, secret, token or
// key with ***, in both the "--flag value" and "--flag=value" forms, and
// keeps every other argument.
func TestWrongDirectoryMasksSecrets(t *testing.T) {
plainEnv(t)
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "summer.yaml"), []byte("binary: acme\n"), 0o644); err != nil {
t.Fatal(err)
}
sub := filepath.Join(root, "bin")
if err := os.MkdirAll(sub, 0o755); err != nil {
t.Fatal(err)
}
fail := errors.New("lagoon: database.dsn is empty")
cases := []struct {
args []string
want string
}{
{[]string{"admin:create", "--email", "a@b.c", "--password", "hunter2"}, "cd ..; {bin} admin:create --email a@b.c --password ***"},
{[]string{"admin:create", "--email", "a@b.c", "--password=hunter2"}, "cd ..; {bin} admin:create --email a@b.c --password=***"},
{[]string{"sync", "--client-secret", "s3cr3t", "--verbose"}, "cd ..; {bin} sync --client-secret *** --verbose"},
{[]string{"sync", "--api-token=t0k3n"}, "cd ..; {bin} sync --api-token=***"},
{[]string{"sync", "--key", "k3y", "acme blog"}, "cd ..; {bin} sync --key *** 'acme blog'"},
{[]string{"sync", "--PASSWD", "hunter2"}, "cd ..; {bin} sync --PASSWD ***"},
{[]string{"sync", "--token", "--force"}, "cd ..; {bin} sync --token --force"},
}
for _, tc := range cases {
got := wrongDirectory("acme", fail, tc.args, sub)
var hint *HintError
if !errors.As(got, &hint) {
t.Fatalf("%q: no hint: %v", tc.args, got)
}
if hint.Try != tc.want {
t.Errorf("%q: Try = %q, want %q", tc.args, hint.Try, tc.want)
}
for _, secret := range []string{"hunter2", "s3cr3t", "t0k3n", "k3y"} {
if strings.Contains(hint.Try, secret) {
t.Errorf("%q: Try line repeats %q: %q", tc.args, secret, hint.Try)
}
}
}
}
// injected are terminal control sequences carried by untrusted text: an
// OSC window title and a CSI screen clear.
var injected = []string{"\x1b]0;pwned\x07", "\x1b[2J", "\x07"}
// carriesInjected reports which injected sequence s still holds.
func carriesInjected(s string) string {
for _, bad := range injected {
if strings.Contains(s, bad) {
return bad
}
}
return ""
}
// TestRenderErrorStripsControl (WR-05): with color on, control sequences
// in the message, explanation, Try lines and the -v chain never reach the
// terminal; the visible text stays.
func TestRenderErrorStripsControl(t *testing.T) {
plainEnv(t)
cause := errors.New("cause \x1b]0;pwned\x07 from the database\x1b[2J")
err := &HintError{
Err: fmt.Errorf("sync failed \x1b]0;pwned\x07 here: %w", cause),
Explanation: "the remote said \x1b[2J no",
Try: "{bin} sync --retry \x1b]0;pwned\x07",
}
var buf bytes.Buffer
RenderError(&buf, "acme", err, Globals{Color: ColorAlways, Verbosity: 1})
out := buf.String()
if bad := carriesInjected(out); bad != "" {
t.Errorf("error block carries %q:\n%q", bad, out)
}
if !strings.Contains(out, "\x1b[") {
t.Errorf("color is off, the test does not exercise styled output:\n%q", out)
}
for _, want := range []string{"sync failed", "the remote said", "sync --retry", "from the database"} {
if !strings.Contains(out, want) {
t.Errorf("error block lacks %q:\n%q", want, out)
}
}
}
// TestSanitize (WR-05): tab stays, CR and LF become spaces, every other
// control character (ESC, BEL, C0, DEL) is removed; clean text is returned
// unchanged. The rules are those of diary's console channel.
func TestSanitize(t *testing.T) {
for in, want := range map[string]string{
"": "",
"plain text ✓": "plain text ✓",
"a\tb": "a\tb",
"line one\nline two\rthree": "line one line two three",
"evil \x1b[31mred\x1b[0m\a bell": "evil [31mred[0m bell",
"a\x1b]0;title\x07b": "a]0;titleb",
"nul\x00 and del\x7f and \x01": "nul and del and ",
"boom\x1b[2Jclear": "boom[2Jclear",
} {
if got := Sanitize(in); got != want {
t.Errorf("Sanitize(%q) = %q, want %q", in, got, want)
}
}
}

View File

@@ -2,6 +2,7 @@ package bonfire
import (
"context"
"errors"
"io"
"os"
"slices"
@@ -23,11 +24,11 @@ const (
)
// Execute runs root with args and returns the process exit code: ExitOK,
// ExitFailure, or ExitUsage for usage errors. With no command word in args
// (only global options, -h or --help) it prints the command list. Errors are
// rendered on the root's error stream by RenderError through fang's error
// handler; fang is used without its version flag, man page and completion
// commands.
// ExitFailure, ExitUsage for usage errors, or the code an error carries
// through WithExitCode. With no command word in args (only global options,
// -h or --help) it prints the command list. Errors are rendered on the
// root's error stream by RenderError through fang's error handler; fang is
// used without its version flag, man page and completion commands.
func Execute(ctx context.Context, root *cobra.Command, args []string) int {
if ctx == nil {
ctx = context.Background()
@@ -73,9 +74,15 @@ func Fail(w io.Writer, name string, err error, g Globals) int {
}
func exitCode(err error) int {
var carrier *exitCodeError
switch {
case err == nil:
return ExitOK
case errors.As(err, &carrier):
if carrier.code < 1 || carrier.code > 255 {
return ExitFailure
}
return carrier.code
case isUsage(err):
return ExitUsage
default:

View File

@@ -73,18 +73,36 @@ func PeekGlobals(args []string) Globals {
}
case strings.HasPrefix(a, "--"+flagEnv+"="):
g.Env = strings.TrimPrefix(a, "--"+flagEnv+"=")
case isVerboseShorthand(a):
g.Verbosity += len(a) - 1
case isShorthandCluster(a):
g.peekShorthands(a[1:])
}
}
return g.normalize()
}
func isVerboseShorthand(a string) bool {
if len(a) < 2 || a[0] != '-' || a[1] == '-' {
return false
// isShorthandCluster reports a single-dash token such as -v, -vvv or -qn.
func isShorthandCluster(a string) bool {
return len(a) >= 2 && a[0] == '-' && a[1] != '-'
}
// peekShorthands reads a cluster of shorthands the way pflag does (-qn is
// -q -n, -vvq is -v -v -q). It stops at the first letter that is not a
// global shorthand: that may be a command flag whose value is the rest of
// the token.
func (g *Globals) peekShorthands(cluster string) {
for _, r := range cluster {
switch r {
case 'v':
g.Verbosity++
case 'q':
g.Quiet = true
case 'n':
g.NoInteraction = true
case 'h':
default:
return
}
}
return strings.Trim(a[1:], "v") == ""
}
func (g Globals) normalize() Globals {

View File

@@ -0,0 +1,123 @@
package bonfire
import (
"bytes"
"context"
"slices"
"strings"
"testing"
)
// peekCases are the global option rows shared by TestPeekGlobals and
// TestGlobalFlagsAgree. cobra marks rows whose args cobra itself accepts
// (it rejects unknown flags, so those rows are PeekGlobals-only).
var peekCases = []struct {
name string
args []string
want Globals
cobra bool
}{
{"none", []string{"migrate"}, Globals{}, true},
{"-v", []string{"-v", "migrate"}, Globals{Verbosity: 1}, true},
{"-vv", []string{"migrate", "-vv"}, Globals{Verbosity: 2}, true},
{"-vvv", []string{"-vvv", "migrate"}, Globals{Verbosity: 3}, true},
{"-vvvv capped", []string{"-vvvv", "migrate"}, Globals{Verbosity: 3}, true},
{"--verbose twice", []string{"--verbose", "migrate", "--verbose"}, Globals{Verbosity: 2}, true},
{"-v -vv capped", []string{"-v", "migrate", "-vv", "--verbose"}, Globals{Verbosity: 3}, true},
{"-q implies -n", []string{"migrate", "-q"}, Globals{Quiet: true, NoInteraction: true}, true},
{"--quiet", []string{"--quiet", "migrate"}, Globals{Quiet: true, NoInteraction: true}, true},
{"-n", []string{"migrate", "-n"}, Globals{NoInteraction: true}, true},
{"--no-interaction", []string{"--no-interaction", "migrate"}, Globals{NoInteraction: true}, true},
{"--ansi", []string{"migrate", "--ansi"}, Globals{Color: ColorAlways}, true},
{"--no-ansi", []string{"--no-ansi", "migrate"}, Globals{Color: ColorNever}, true},
{"--ansi then --no-ansi", []string{"--ansi", "migrate", "--no-ansi"}, Globals{Color: ColorNever}, true},
{"--no-ansi then --ansi", []string{"--no-ansi", "migrate", "--ansi"}, Globals{Color: ColorAlways}, true},
{"--env X", []string{"--env", "staging", "migrate"}, Globals{Env: "staging"}, true},
{"--env=X", []string{"migrate", "--env=production"}, Globals{Env: "production"}, true},
{"--env takes a dash value", []string{"migrate", "--env", "-v"}, Globals{Env: "-v"}, true},
{"last --env wins", []string{"--env=a", "migrate", "--env", "b"}, Globals{Env: "b"}, true},
{"stop at --", []string{"migrate", "--", "-v", "-q", "--env=x"}, Globals{}, true},
{"combined -qn", []string{"migrate", "-qn"}, Globals{Quiet: true, NoInteraction: true}, true},
{"combined -vq", []string{"-vq", "migrate"}, Globals{Verbosity: 1, Quiet: true, NoInteraction: true}, true},
{"combined -nvv", []string{"migrate", "-nvv"}, Globals{Verbosity: 2, NoInteraction: true}, true},
{"everything", []string{"-vv", "--env=dev", "migrate", "-n", "--no-ansi"}, Globals{Verbosity: 2, NoInteraction: true, Color: ColorNever, Env: "dev"}, true},
{"unknown flags ignored", []string{"--bogus", "-x", "migrate", "-", "--env"}, Globals{}, false},
{"--env without a value", []string{"migrate", "--env"}, Globals{}, false},
{"empty", nil, Globals{}, false},
}
// TestPeekGlobals: the tolerant pre-boot scan reads every global option
// form, caps verbosity, lets the last color flag win, stops at -- and
// never fails on unknown tokens.
func TestPeekGlobals(t *testing.T) {
for _, tc := range peekCases {
t.Run(tc.name, func(t *testing.T) {
if got := PeekGlobals(tc.args); got != tc.want {
t.Errorf("PeekGlobals(%q) = %+v, want %+v", tc.args, got, tc.want)
}
})
}
}
// TestGlobalsArgs: Args re-serialises Globals so PeekGlobals reads them
// back unchanged.
func TestGlobalsArgs(t *testing.T) {
cases := []struct {
g Globals
want string
}{
{Globals{}, ""},
{Globals{Verbosity: 1}, "-v"},
{Globals{Verbosity: 3}, "-vvv"},
{Globals{Verbosity: 7}, "-vvv"},
{Globals{Quiet: true, NoInteraction: true}, "-q"},
{Globals{NoInteraction: true}, "-n"},
{Globals{Color: ColorAlways}, "--ansi"},
{Globals{Color: ColorNever}, "--no-ansi"},
{Globals{Env: "staging"}, "--env=staging"},
{Globals{Verbosity: 2, Quiet: true, NoInteraction: true, Color: ColorNever, Env: "dev"}, "-vv -q --no-ansi --env=dev"},
}
for _, tc := range cases {
args := tc.g.Args()
if got := strings.Join(args, " "); got != tc.want {
t.Errorf("%+v.Args() = %q, want %q", tc.g, got, tc.want)
}
if back := PeekGlobals(args); back != tc.g.normalize() {
t.Errorf("PeekGlobals(%q) = %+v, want %+v", args, back, tc.g.normalize())
}
}
}
// TestGlobalFlagsAgree: for every row cobra accepts, the OnGlobals hook
// sees exactly the Globals PeekGlobals read before the root existed.
func TestGlobalFlagsAgree(t *testing.T) {
for _, tc := range peekCases {
if !tc.cobra {
continue
}
t.Run(tc.name, func(t *testing.T) {
var hooked []Globals
var seen Globals
cmd := Command{Name: "migrate", Description: "Run the migrations", Run: func(_ context.Context, _ Input, out Output) error {
seen = GlobalsOf(out)
return nil
}}
var buf bytes.Buffer
root, err := NewRootIO("acme", []Command{cmd}, strings.NewReader(""), &buf, &buf,
WithVersion(goldenVersion), OnGlobals(func(g Globals) { hooked = append(hooked, g) }))
if err != nil {
t.Fatal(err)
}
if code := Execute(context.Background(), root, slices.Clone(tc.args)); code != ExitOK {
t.Fatalf("exit %d:\n%s", code, buf.String())
}
peek := PeekGlobals(tc.args)
if len(hooked) != 1 || hooked[0] != peek {
t.Errorf("hook saw %+v, PeekGlobals %+v", hooked, peek)
}
if seen != peek {
t.Errorf("GlobalsOf(out) = %+v, PeekGlobals %+v", seen, peek)
}
})
}
}

View File

@@ -0,0 +1,268 @@
package bonfire
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
var update = flag.Bool("update", false, "rewrite golden files")
// goldenVersion is the fixed version line of every golden.
var goldenVersion = Version{App: "v1.4.0", Framework: "v0.3.2"}
// plainEnv clears the environment variables that force or detect color, so
// goldens render the same on every machine: no escape codes unless --ansi.
func plainEnv(t *testing.T) {
t.Helper()
for _, k := range []string{"FORCE_COLOR", "CLICOLOR_FORCE", "CLICOLOR", "COLORTERM", "NO_COLOR"} {
t.Setenv(k, "")
_ = os.Unsetenv(k)
}
t.Setenv("TERM", "dumb")
}
// goldenTokenCommand is the admin:token:create command of the D-14
// preview: value, bare and repeatable flags with defaults, long help and
// two examples. The values are fake.
func goldenTokenCommand() Command {
return Command{
Name: "admin:token:create",
Description: "Create a personal API token for a backend admin",
Long: "Creates a token for the admin with the given login and prints it once.\nStore it in the client right away; it cannot be shown again.",
Args: []Arg{{Name: "login", Description: "The admin's login", Required: true}},
Flags: []Flag{
{Name: "scopes", Description: "Comma-separated scopes", Default: "*"},
{Name: "expires", Description: "Days until expiry", Default: "90"},
{Name: "json", Description: "Print the token as JSON", Bare: true},
{Name: "tag", Shorthand: "t", Description: "A tag for the token", Repeatable: true},
},
Examples: []Example{
{Command: "admin:token:create jane --scopes=journal.*", Description: "Token for the MCP server, journal only"},
{Command: "admin:token:create jane --json", Description: "Script-friendly output"},
},
Run: func(context.Context, Input, Output) error { return nil },
}
}
// goldenCommands is the command set of the list goldens (D-13 preview plus
// a hidden and a passthrough command).
func goldenCommands() []Command {
ok := func(context.Context, Input, Output) error { return nil }
return []Command{
{Name: "migrate", Description: "Run the database migrations", Run: ok},
{Name: "serve", Description: "Serve the application", Run: ok},
{Name: "admin:create", Description: "Create a backend administrator", Run: ok},
goldenTokenCommand(),
{Name: "make:command", Description: "Create a new console command", Run: ok},
{Name: "make:model", Description: "Create a new model and migration", Run: ok},
{Name: "acme:secret", Description: "Rotate the internal signing secret", Hidden: true, Run: ok},
goldenPassthroughCommand(),
}
}
// goldenPassthroughCommand forwards everything after its name.
func goldenPassthroughCommand() Command {
return Command{
Name: "tool:exec",
Description: "Run the bundled tool with its own arguments",
Long: "Everything after the command name reaches the tool unchanged, global flags included.",
Passthrough: true,
Args: []Arg{{Name: "args", Description: "The tool's arguments"}},
Examples: []Example{{Command: "tool:exec --version", Description: "The tool's own version"}},
Run: func(context.Context, Input, Output) error { return nil },
}
}
// runGolden executes args on a fresh root over commands and returns stdout
// and stderr.
func runGolden(t *testing.T, commands []Command, args ...string) (string, string, int) {
t.Helper()
var out, errW bytes.Buffer
root, err := NewRootIO("acme", commands, strings.NewReader(""), &out, &errW, WithVersion(goldenVersion))
if err != nil {
t.Fatal(err)
}
code := Execute(context.Background(), root, args)
return out.String(), errW.String(), code
}
// assertGolden compares got with testdata/<name>.golden, rewriting the file
// first when -update is set.
func assertGolden(t *testing.T, name string, got []byte) {
t.Helper()
path := filepath.Join("testdata", name+".golden")
if *update {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, got, 0o644); err != nil {
t.Fatal(err)
}
}
want, err := os.ReadFile(path)
if err != nil {
t.Fatalf("%s: %v (run go test -run %s -update to create it)", name, err, t.Name())
}
if !bytes.Equal(got, want) {
t.Errorf("%s differs from %s\n%s", name, path, firstDiff(want, got))
}
}
// firstDiff prints the first differing line of want and got with a little
// context, in a unified-diff-like form.
func firstDiff(want, got []byte) string {
wl := strings.Split(string(want), "\n")
gl := strings.Split(string(got), "\n")
for i := 0; i < max(len(wl), len(gl)); i++ {
var w, g string
if i < len(wl) {
w = wl[i]
}
if i < len(gl) {
g = gl[i]
}
if w == g {
continue
}
var b strings.Builder
for j := max(0, i-2); j < i; j++ {
fmt.Fprintf(&b, " %q\n", wl[j])
}
fmt.Fprintf(&b, "- %q\n+ %q\n", w, g)
return b.String()
}
return "(same lines, different line endings)"
}
// TestListGolden: the bare binary prints the D-13 list.
func TestListGolden(t *testing.T) {
plainEnv(t)
out, errOut, code := runGolden(t, goldenCommands())
if code != ExitOK || errOut != "" {
t.Fatalf("exit %d, stderr %q", code, errOut)
}
assertGolden(t, "list", []byte(out))
for _, args := range [][]string{{"list"}, {"--help"}, {"-h"}, {"help"}, {"-v"}, {"--no-ansi"}} {
again, _, code := runGolden(t, goldenCommands(), args...)
if code != ExitOK || again != out {
t.Errorf("%v: exit %d, output differs from the bare list:\n%s", args, code, firstDiff([]byte(out), []byte(again)))
}
}
}
// TestListAllGolden: list --all adds the hidden command.
func TestListAllGolden(t *testing.T) {
plainEnv(t)
out, _, code := runGolden(t, goldenCommands(), "list", "--all")
if code != ExitOK {
t.Fatalf("exit %d", code)
}
assertGolden(t, "list_all", []byte(out))
}
// TestListJSONGolden: list --format=json prints the catalog.
func TestListJSONGolden(t *testing.T) {
plainEnv(t)
out, _, code := runGolden(t, goldenCommands(), "list", "--format=json")
if code != ExitOK {
t.Fatalf("exit %d", code)
}
assertGolden(t, "list.json", []byte(out))
}
// TestListStyledGolden: --ansi styles the list in the palette (ANSI256),
// so a palette change shows up in review.
func TestListStyledGolden(t *testing.T) {
plainEnv(t)
out, _, code := runGolden(t, goldenCommands(), "--ansi")
if code != ExitOK {
t.Fatalf("exit %d", code)
}
if !strings.Contains(out, "\x1b[") {
t.Fatalf("--ansi list has no escape codes:\n%q", out)
}
assertGolden(t, "list_styled", []byte(out))
}
// TestHelpGolden: admin:token:create --help in the D-14 layout.
func TestHelpGolden(t *testing.T) {
plainEnv(t)
out, _, code := runGolden(t, goldenCommands(), "admin:token:create", "--help")
if code != ExitOK {
t.Fatalf("exit %d", code)
}
assertGolden(t, "help_token_create", []byte(out))
}
// TestHelpPassthroughGolden: the help of a passthrough command, reached
// through help <command> because --help after its name is forwarded.
func TestHelpPassthroughGolden(t *testing.T) {
plainEnv(t)
out, _, code := runGolden(t, goldenCommands(), "help", "tool:exec")
if code != ExitOK {
t.Fatalf("exit %d", code)
}
assertGolden(t, "help_passthrough", []byte(out))
}
// TestHelpEqualsHelpFlag: help <command>, <command> --help and <command>
// -h print the same bytes, with or without global flags.
func TestHelpEqualsHelpFlag(t *testing.T) {
plainEnv(t)
for _, name := range []string{"admin:token:create", "migrate", "list", "help"} {
want, _, code := runGolden(t, goldenCommands(), "help", name)
if code != ExitOK || !strings.Contains(want, "Usage:\n") {
t.Fatalf("help %s: exit %d\n%s", name, code, want)
}
for _, args := range [][]string{{name, "--help"}, {name, "-h"}, {"-v", "help", name}, {"--no-ansi", name, "--help"}} {
got, _, code := runGolden(t, goldenCommands(), args...)
if code != ExitOK || got != want {
t.Errorf("%v: exit %d, differs from help %s:\n%s", args, code, name, firstDiff([]byte(want), []byte(got)))
}
}
}
}
// TestErrorGolden: the error block of D-18 for a hint with {bin}, an
// unknown command with a suggestion and the -v chain, on stderr.
func TestErrorGolden(t *testing.T) {
plainEnv(t)
hinted := Command{Name: "make:plugin", Description: "Create a plugin", Run: func(context.Context, Input, Output) error {
return WithHint(errors.New("No application found in /home/jane/acme/plugins."),
"summer make:command needs an application root (summer.yaml).",
"cd ..; {bin} make:command acme.blog:publish")
}}
chained := Command{Name: "db:check", Description: "Check the database", Run: func(context.Context, Input, Output) error {
return fmt.Errorf("db:check: %w", fmt.Errorf("connect to 127.0.0.1:5432: %w", errors.New("connection refused")))
}}
commands := append(goldenCommands(), hinted, chained)
cases := []struct {
golden string
args []string
code int
}{
{"error_hint", []string{"make:plugin"}, ExitFailure},
{"error_unknown_command", []string{"migrat"}, ExitUsage},
{"error_chain_verbose", []string{"-v", "db:check"}, ExitFailure},
}
for _, tc := range cases {
t.Run(tc.golden, func(t *testing.T) {
out, errOut, code := runGolden(t, commands, tc.args...)
if code != tc.code {
t.Fatalf("exit %d, want %d", code, tc.code)
}
if out != "" {
t.Errorf("the error block reached stdout:\n%s", out)
}
assertGolden(t, tc.golden, []byte(errOut))
})
}
}

View File

@@ -149,7 +149,7 @@ func Process(ctx context.Context, out Output, operation string, items []string,
}
if c, ok := out.(*console); ok && c.live() && len(items) > 0 {
var errs []error
_ = runLive(ctx, c.out, c, newProcessModel(c.palette, operation, items), func(send func(tea.Msg)) error {
started, _ := runLive(ctx, c.out, c, newProcessModel(c.palette, operation, items), func(send func(tea.Msg)) error {
for i, item := range items {
if err := ctx.Err(); err != nil {
errs = append(errs, err)
@@ -165,7 +165,9 @@ func Process(ctx context.Context, out Output, operation string, items []string,
}
return nil
})
return errors.Join(errs...)
if started {
return errors.Join(errs...)
}
}
var errs []error
for _, item := range items {
@@ -175,7 +177,7 @@ func Process(ctx context.Context, out Output, operation string, items []string,
}
if err := fn(ctx, item); err != nil {
errs = append(errs, failure(item, err))
out.Error(failureLine(item, err))
out.Error(failureText(item, err))
continue
}
out.Success(item)

View File

@@ -4,14 +4,20 @@ package bonfire
// process.go and progress.go) to bubbletea v2, without SSU's git and
// engine types. Each runs as a bubbletea program with input off (stdin
// stays in cooked mode, so Ctrl+C reaches the command's own signal
// handling), no signal handler of its own and the command's context.
// handling), without bubbletea's signal handler and with the command's
// context. While a view runs, runLive relays SIGINT and SIGTERM only to
// restore the terminal before the signal takes its usual effect.
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"charm.land/bubbles/v2/progress"
"charm.land/bubbles/v2/spinner"
@@ -46,10 +52,18 @@ type (
}
// finishMsg ends a live view; err is the result of the work.
finishMsg struct{ err error }
// printAboveMsg is a line to print above the view.
printAboveMsg string
)
// printLine is the message that prints s, one or more lines, above the
// running view. It is bubbletea's own Println message, sent straight to
// the program's queue: the event loop prints it itself, in the order the
// messages arrive. Returning tea.Println as a command instead would run
// it on a goroutine of its own, so lines would race each other and a line
// still in flight when the view quits would be lost.
func printLine(s string) tea.Msg {
return tea.Println(s)()
}
// itemState is the display state of one item in the process view.
type itemState int
@@ -126,7 +140,7 @@ func (m spinnerModel) View() tea.View {
line += fmt.Sprintf(" %d/%d", m.done, m.total)
}
if m.current != "" {
line += " " + p.Muted.Render(m.current+"…")
line += " " + p.Muted.Render(Sanitize(m.current)+"…")
}
if m.failed > 0 {
line += " " + p.ErrorBold.Render(fmt.Sprintf("! %d failed", m.failed))
@@ -179,7 +193,7 @@ func (m progressModel) View() tea.View {
}
line := " " + m.bar.ViewAs(pct) + m.palette.Muted.Render(fmt.Sprintf(" %d/%d", m.done, m.total))
if m.current != "" && !m.finished {
line += " " + m.palette.Muted.Render(m.current)
line += " " + m.palette.Muted.Render(Sanitize(m.current))
}
if m.failed > 0 {
line += " " + m.palette.ErrorBold.Render(fmt.Sprintf("! %d failed", m.failed))
@@ -275,17 +289,18 @@ func (m processModel) View() tea.View {
var b strings.Builder
start, end := m.window()
for _, it := range m.items[start:end] {
name := Sanitize(it.name)
switch it.state {
case itemWaiting:
b.WriteString(p.Muted.Render(" "+symWaiting) + " " + p.Muted.Render(it.name))
b.WriteString(p.Muted.Render(" "+symWaiting) + " " + p.Muted.Render(name))
case itemRunning:
b.WriteString(" " + m.spin.View() + " " + it.name)
b.WriteString(" " + m.spin.View() + " " + name)
case itemDone:
b.WriteString(p.Success.Render(" "+symCheck) + " " + it.name)
b.WriteString(p.Success.Render(" "+symCheck) + " " + name)
case itemFailed:
b.WriteString(p.Error.Render(" "+symCross) + " " + it.name)
b.WriteString(p.Error.Render(" "+symCross) + " " + name)
if it.err != nil {
b.WriteString(" " + p.Error.Render(it.err.Error()))
b.WriteString(" " + p.Error.Render(Sanitize(it.err.Error())))
}
}
b.WriteString("\n")
@@ -306,82 +321,167 @@ func (m processModel) View() tea.View {
return tea.NewView(b.String())
}
// liveModel wraps a view so lines from PrintAbove print above it.
type liveModel struct {
inner tea.Model
// liveView is the running live program and the console that started it.
type liveView struct {
owner *console
prog *tea.Program
}
func (m liveModel) Init() tea.Cmd { return m.inner.Init() }
func (m liveModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if line, ok := msg.(printAboveMsg); ok {
return m, tea.Println(string(line))
}
inner, cmd := m.inner.Update(msg)
m.inner = inner
return m, cmd
}
func (m liveModel) View() tea.View { return m.inner.View() }
// The running live program, for PrintAbove. Only one live view runs at a
// time; widgets started while one runs print plain lines.
// The running live view, for PrintAbove and the owning console's output.
// Only one live view runs at a time; a widget that cannot claim the slot
// (a nested widget, or a second console at the same moment) prints plain
// lines.
var (
liveMu sync.Mutex
activeProg *tea.Program
activeView *liveView
)
// liveActive reports whether a live view is running.
func liveActive() bool {
liveMu.Lock()
defer liveMu.Unlock()
return activeProg != nil
}
// errLivePanic is the result the final frame shows when work panicked.
var errLivePanic = errors.New("bonfire: live view work panicked")
// runLive runs model as a bubbletea program on out while work runs on the
// calling goroutine. work gets a send function for progress messages. When
// work returns, the program is unregistered (so PrintAbove stops sending to
// it), told to finish and waited for; it never receives lines after that.
func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, work func(send func(tea.Msg)) error) error {
// calling goroutine, with c as the owner of the view. It first claims the
// single live slot under liveMu; when another view holds it, runLive
// returns started false without running work, and the caller prints plain
// lines instead. work gets a send function for progress messages. When
// work returns or panics, the view is unregistered (so PrintAbove stops
// sending to it), told to finish and waited for, all under liveMu; it
// never receives lines after that, and a panic goes on after the cleanup.
func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, work func(send func(tea.Msg)) error) (started bool, err error) {
if ctx == nil {
ctx = context.Background()
}
prog := tea.NewProgram(liveModel{inner: model},
prog := tea.NewProgram(model,
tea.WithInput(nil),
tea.WithOutput(out),
tea.WithoutSignalHandler(),
tea.WithContext(ctx),
tea.WithColorProfile(c.profile()),
)
view := &liveView{owner: c, prog: prog}
liveMu.Lock()
if activeView != nil {
liveMu.Unlock()
return false, nil
}
activeView = view
liveMu.Unlock()
finished := make(chan struct{})
go func() {
defer close(finished)
_, _ = prog.Run()
}()
liveMu.Lock()
activeProg = prog
liveMu.Unlock()
stopSignals := watchSignals(view, finished)
err := work(prog.Send)
liveMu.Lock()
activeProg = nil
liveMu.Unlock()
prog.Send(finishMsg{err: err})
<-finished
return err
completed := false
defer func() {
result := err
if !completed {
result = errLivePanic
}
// Unregister, finish and wait as one step: a printer that arrives
// meanwhile waits for the lock and then writes after the final
// frame, so no line reaches a stopped program or races the last
// frame.
liveMu.Lock()
if activeView == view {
activeView = nil
}
prog.Send(finishMsg{err: result})
<-finished
liveMu.Unlock()
stopSignals()
}()
err = work(prog.Send)
completed = true
return true, err
}
// printAboveLines prints s, one or more lines, above the running live view.
// It reports false when no view runs.
// watchSignals relays SIGINT and SIGTERM (those not ignored) while view
// runs. The views run without bubbletea's own signal handling, so without
// this a Ctrl+C would end the process with the cursor still hidden. On a
// signal the program is killed (bubbletea then restores the cursor and the
// terminal), the view is unregistered and the signal is raised again on
// the process with the relay stopped, so its default action, or the
// command's own handler, still applies. The returned function stops the
// relay when the view ends normally; a signal that arrived meanwhile is
// raised again too.
func watchSignals(view *liveView, finished <-chan struct{}) (stop func()) {
var relay []os.Signal
for _, sig := range []os.Signal{os.Interrupt, syscall.SIGTERM} {
if !signal.Ignored(sig) {
relay = append(relay, sig)
}
}
if len(relay) == 0 {
return func() {}
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, relay...)
quit := make(chan struct{})
done := make(chan struct{})
go func() {
defer close(done)
select {
case sig := <-sigs:
view.prog.Kill()
<-finished
liveMu.Lock()
if activeView == view {
activeView = nil
}
liveMu.Unlock()
signal.Stop(sigs)
raise(sig)
case <-quit:
}
}()
return func() {
close(quit)
<-done
signal.Stop(sigs)
select {
case sig := <-sigs:
raise(sig)
default:
}
}
}
// raise sends sig to the own process; when that fails, it exits with 128
// plus the signal number, as a shell reports a death by that signal.
func raise(sig os.Signal) {
p, err := os.FindProcess(os.Getpid())
if err == nil {
err = p.Signal(sig)
}
if err != nil {
code := 1
if s, ok := sig.(syscall.Signal); ok {
code = 128 + int(s)
}
os.Exit(code)
}
}
// printAboveLines prints s, one or more lines, above the running live view,
// whichever console started it (bonfire.PrintAbove). It reports false when
// no view runs.
func printAboveLines(s string) bool {
return printAbove(nil, s)
}
// printAbove prints s above the running live view when owner is nil or is
// the console that started the view, and reports whether it did.
func printAbove(owner *console, s string) bool {
liveMu.Lock()
defer liveMu.Unlock()
if activeProg == nil {
if activeView == nil || (owner != nil && activeView.owner != owner) {
return false
}
for _, line := range strings.Split(strings.TrimSuffix(s, "\n"), "\n") {
activeProg.Send(printAboveMsg(line))
}
// One message for the whole block, sent while holding liveMu: blocks
// from different goroutines keep the order of their calls.
activeView.prog.Send(printLine(strings.TrimSuffix(s, "\n")))
return true
}

View File

@@ -0,0 +1,108 @@
//go:build unix
package bonfire
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"testing"
"time"
)
const (
hideCursor = "\x1b[?25l"
showCursor = "\x1b[?25h"
)
// TestLiveSignalHelper is the helper process of
// TestLiveViewRestoresTerminalOnSignal: with BONFIRE_LIVE_SIGNAL_HELPER set
// it runs a spinner on a terminal console over its stdout, prints ready on
// stderr and waits. In own_handler mode it also holds its own signal
// context and exits 0 once that context ends.
func TestLiveSignalHelper(t *testing.T) {
mode := os.Getenv("BONFIRE_LIVE_SIGNAL_HELPER")
if mode == "" {
return
}
ctx := context.Background()
if mode == "own_handler" {
var stop context.CancelFunc
ctx, stop = signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
}
c := newConsole(strings.NewReader(""), os.Stdout, os.Stderr, noEnv, boolPtr(true), boolPtr(true))
_ = c.Spinner("waiting for a signal", func() error {
fmt.Fprintln(os.Stderr, "ready")
select {
case <-ctx.Done():
case <-time.After(20 * time.Second):
}
return nil
})
if mode == "own_handler" && ctx.Err() != nil {
os.Exit(0)
}
os.Exit(3)
}
// TestLiveViewRestoresTerminalOnSignal (WR-01): SIGINT during a live view
// shows the cursor again and then still ends the process by the signal,
// or reaches the command's own signal handler when it has one.
func TestLiveViewRestoresTerminalOnSignal(t *testing.T) {
for _, mode := range []string{"default", "own_handler"} {
t.Run(mode, func(t *testing.T) {
var stdout, stderr lockedBuffer
cmd := exec.Command(os.Args[0], "-test.run=^TestLiveSignalHelper$")
cmd.Env = append(os.Environ(), "BONFIRE_LIVE_SIGNAL_HELPER="+mode)
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(10 * time.Second)
for !strings.Contains(stderr.String(), "ready") || !strings.Contains(stdout.String(), hideCursor) {
if time.Now().After(deadline) {
_ = cmd.Process.Kill()
t.Fatalf("helper not ready; stdout %q, stderr %q", stdout.String(), stderr.String())
}
time.Sleep(10 * time.Millisecond)
}
if err := cmd.Process.Signal(os.Interrupt); err != nil {
t.Fatal(err)
}
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
var err error
select {
case err = <-done:
case <-time.After(10 * time.Second):
_ = cmd.Process.Kill()
t.Fatalf("helper did not end after SIGINT; stdout %q", stdout.String())
}
out := stdout.String()
if i := strings.LastIndex(out, hideCursor); !strings.Contains(out[i:], showCursor) {
t.Errorf("cursor left hidden: no show-cursor after the last hide-cursor in %q", out)
}
switch mode {
case "default":
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("helper exited cleanly (%v), want death by SIGINT", err)
}
ws, ok := ee.Sys().(syscall.WaitStatus)
if !ok || !ws.Signaled() || ws.Signal() != syscall.SIGINT {
t.Errorf("helper ended with %v, want death by SIGINT", err)
}
case "own_handler":
if err != nil {
t.Errorf("helper with its own handler ended with %v, want exit 0; stderr %q", err, stderr.String())
}
}
})
}
}

View File

@@ -0,0 +1,551 @@
package bonfire
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"regexp"
"strings"
"sync"
"testing"
"time"
tea "charm.land/bubbletea/v2"
"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/ansi"
)
// TestLiveModels: the spinner, progress, process and selector models
// render the right frames through Update and View, without a terminal; one
// program runs end to end into a buffer with input off.
func TestLiveModels(t *testing.T) {
p := DefaultPalette()
size := tea.WindowSizeMsg{Width: 80, Height: 24}
t.Run("spinner", func(t *testing.T) {
var m tea.Model = newSpinnerModel(p, "Syncing")
if m.Init() == nil {
t.Fatal("spinner does not tick")
}
first := frame(m)
m, cmd := m.Update(m.(spinnerModel).spin.Tick())
if cmd == nil || frame(m) == first {
t.Errorf("tick did not advance the spinner: %q -> %q", first, frame(m))
}
m, _ = m.Update(itemMsg{done: 1, total: 3, item: "post-1"})
m, _ = m.Update(itemMsg{done: 2, total: 3, item: "post-2", err: errors.New("x")})
v := frame(m)
for _, want := range []string{"Syncing 2/3", "post-2…", "! 1 failed"} {
if !strings.Contains(v, want) {
t.Errorf("spinner frame lacks %q: %q", want, v)
}
}
done, cmd := m.Update(finishMsg{})
if cmd == nil || frame(done) != symCheck+" Syncing\n" {
t.Errorf("success frame = %q", frame(done))
}
failed, _ := m.Update(finishMsg{err: errors.New("no")})
if frame(failed) != symCross+" Syncing\n" {
t.Errorf("failure frame = %q", frame(failed))
}
})
t.Run("progress", func(t *testing.T) {
var m tea.Model = newProgressModel(p, 10)
if m.Init() != nil {
t.Error("progress has an init command")
}
m, _ = m.Update(tea.WindowSizeMsg{Width: 20, Height: 5})
m, _ = m.Update(itemMsg{done: 0, total: 10})
if v := frame(m); !strings.Contains(v, "0/10") {
t.Errorf("empty bar = %q", v)
}
m, _ = m.Update(itemMsg{done: 4, total: 10, item: "row 4"})
m, _ = m.Update(itemMsg{done: 5, total: 10})
if v := frame(m); !strings.Contains(v, "5/10") || !strings.Contains(v, "row 4") {
t.Errorf("Current kept across steps: %q", v)
}
m, _ = m.Update(itemMsg{done: 5, total: 10, item: "row 5", err: errors.New("bad")})
if v := frame(m); !strings.Contains(v, "! 1 failed") || !strings.Contains(v, "row 5") {
t.Errorf("Fail frame = %q", v)
}
m, cmd := m.Update(finishMsg{})
v := frame(m)
if cmd == nil || strings.Contains(v, "row 5") || !strings.HasSuffix(v, "\n") {
t.Errorf("final frame = %q", v)
}
zero := newProgressModel(p, 0)
if v := frame(zero); !strings.Contains(v, "0/0") {
t.Errorf("zero total frame = %q", v)
}
})
t.Run("process", func(t *testing.T) {
var m tea.Model = newProcessModel(p, "", []string{"blog", "shop", "forum"})
m, _ = m.Update(size)
v := frame(m)
for _, name := range []string{"blog", "shop", "forum"} {
if !strings.Contains(v, symWaiting+" "+name) {
t.Errorf("pending row %s missing: %q", name, v)
}
}
if !strings.Contains(v, "Processing 0/3") || !strings.Contains(v, "0%") {
t.Errorf("default operation label missing: %q", v)
}
m, _ = m.Update(stepMsg{item: "blog", state: itemRunning, done: 0})
if v := frame(m); strings.Contains(v, symWaiting+" blog") {
t.Errorf("running row still pending: %q", v)
}
m, _ = m.Update(stepMsg{item: "blog", state: itemDone, done: 1})
m, _ = m.Update(stepMsg{item: "blog", state: itemRunning, done: 1})
m, _ = m.Update(stepMsg{item: "shop", state: itemFailed, err: errors.New("rejected"), done: 2})
v = frame(m)
for _, want := range []string{symCheck + " blog", symCross + " shop rejected", symWaiting + " forum", "Processing 2/3", "66%"} {
if !strings.Contains(v, want) {
t.Errorf("process frame lacks %q:\n%s", want, v)
}
}
m, cmd := m.Update(finishMsg{})
if cmd == nil || !strings.HasSuffix(frame(m), "\n") {
t.Errorf("final process frame = %q", frame(m))
}
// Ten rows on a six-line terminal: three rows around the running one.
var items []string
for i := range 10 {
items = append(items, fmt.Sprintf("item-%d", i))
}
var big tea.Model = newProcessModel(p, "Pushing", items)
big, _ = big.Update(tea.WindowSizeMsg{Width: 80, Height: 6})
big, _ = big.Update(stepMsg{item: "item-6", state: itemRunning, done: 6})
v = frame(big)
for _, want := range []string{"item-5", "item-6", "item-7"} {
if !strings.Contains(v, want) {
t.Errorf("window lacks %s:\n%s", want, v)
}
}
for _, gone := range []string{"item-0", "item-4", "item-8"} {
if strings.Contains(v, gone+"\n") || strings.Contains(v, gone+" ") {
t.Errorf("window shows %s:\n%s", gone, v)
}
}
})
t.Run("selector", func(t *testing.T) {
key := func(m tea.Model, msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { return m.Update(msg) }
up, down := tea.KeyPressMsg{Code: tea.KeyUp}, tea.KeyPressMsg{Code: tea.KeyDown}
enter, esc := tea.KeyPressMsg{Code: tea.KeyEnter}, tea.KeyPressMsg{Code: tea.KeyEscape}
ctrlC := tea.KeyPressMsg{Code: 'c', Mod: tea.ModCtrl}
var m tea.Model = newSelectorModel(p, "Pick a plugin", []string{"blog", "shop", "forum"}, 1)
m, _ = m.Update(size)
if v := frame(m); !strings.Contains(v, "> shop") || !strings.Contains(v, "? Pick a plugin") {
t.Errorf("default cursor frame = %q", v)
}
m, _ = key(m, up)
m, _ = key(m, up)
m, _ = key(m, tea.KeyPressMsg{Code: 'k', Text: "k"})
if c := m.(selectorModel).cursor; c != 0 {
t.Errorf("cursor moved above the top: %d", c)
}
for range 5 {
m, _ = key(m, down)
}
if c := m.(selectorModel).cursor; c != 2 {
t.Errorf("cursor moved below the bottom: %d", c)
}
m, _ = key(m, enter)
if v := frame(m); !strings.Contains(v, "Selected:") || !strings.Contains(v, "forum") || !strings.Contains(v, "esc back") {
t.Errorf("confirmation frame = %q", v)
}
m, _ = key(m, esc)
if m.(selectorModel).confirming || m.(selectorModel).cancelled {
t.Error("esc on the confirmation screen did not go back")
}
m, _ = key(m, tea.KeyPressMsg{Code: 'k', Text: "k"})
m, _ = key(m, enter)
m, cmd := key(m, enter)
sel := m.(selectorModel)
if cmd == nil || sel.chosen != 1 || sel.cancelled {
t.Fatalf("selector chose %d (cancelled %v)", sel.chosen, sel.cancelled)
}
if v := frame(m); v != "? Pick a plugin shop\n" {
t.Errorf("summary line = %q", v)
}
for name, keys := range map[string][]tea.KeyPressMsg{
"esc in the list": {esc},
"ctrl+c in the list": {ctrlC},
"ctrl+c on confirmation": {enter, ctrlC},
} {
var s tea.Model = newSelectorModel(p, "Pick", []string{"a", "b"}, 9)
var cmd tea.Cmd
for _, k := range keys {
s, cmd = key(s, k)
}
if !s.(selectorModel).cancelled || cmd == nil || s.(selectorModel).chosen != -1 {
t.Errorf("%s: not cancelled", name)
}
if v := frame(s); v != "? Pick\n" {
t.Errorf("%s: summary line = %q", name, v)
}
}
// A short terminal shows a window that follows the cursor.
var long []string
for i := range 20 {
long = append(long, fmt.Sprintf("choice-%02d", i))
}
var w tea.Model = newSelectorModel(p, "Pick", long, 0)
w, _ = w.Update(tea.WindowSizeMsg{Width: 80, Height: 8})
for range 10 {
w, _ = key(w, down)
}
v := frame(w)
if !strings.Contains(v, "> choice-10") || strings.Contains(v, "choice-00") || strings.Contains(v, "choice-11") {
t.Errorf("selector window:\n%s", v)
}
})
t.Run("print above", func(t *testing.T) {
// A printed block is one message for the program's queue, not a
// command bubbletea would run on a goroutine of its own.
msg := printLine("first\nsecond")
if msg == nil {
t.Fatal("printLine returned no message")
}
if _, isCmd := msg.(tea.Cmd); isCmd {
t.Error("printLine returned a command")
}
})
t.Run("program end to end", func(t *testing.T) {
var buf bytes.Buffer
prog := tea.NewProgram(newSpinnerModel(p, "Importing posts"),
tea.WithInput(nil),
tea.WithOutput(&buf),
tea.WithoutSignalHandler(),
tea.WithWindowSize(80, 24),
tea.WithColorProfile(colorprofile.NoTTY),
)
done := make(chan error, 1)
go func() {
_, err := prog.Run()
done <- err
}()
prog.Send(itemMsg{done: 1, total: 2, item: "first"})
prog.Send(printLine("imported first"))
prog.Send(itemMsg{done: 2, total: 2, item: "second"})
prog.Send(finishMsg{})
select {
case err := <-done:
if err != nil {
t.Fatalf("program: %v", err)
}
case <-time.After(10 * time.Second):
prog.Kill()
t.Fatal("program did not finish")
}
out := ansi.Strip(buf.String())
for _, want := range []string{"imported first", symCheck + " Importing posts"} {
if !strings.Contains(out, want) {
t.Errorf("program output lacks %q:\n%q", want, out)
}
}
})
}
// lockedBuffer is a bytes.Buffer safe for a live program and other
// goroutines writing at the same time.
type lockedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *lockedBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *lockedBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
var numberedLine = regexp.MustCompile(`(line|bg)-\d{3}`)
// numbered returns the numbered lines of the ANSI-stripped output in the
// order they appear.
func numbered(out string) []string {
return numberedLine.FindAllString(ansi.Strip(out), -1)
}
// printNumbered prints line-<from> .. line-<to-1> through the console's
// print paths in turn: Println, Printf, Info and bonfire.PrintAbove.
func printNumbered(c *console, from, to int) {
for i := from; i < to; i++ {
line := fmt.Sprintf("line-%03d", i)
switch i % 4 {
case 0:
c.Println(line)
case 1:
c.Printf("%s\n", line)
case 2:
c.Info(line)
case 3:
if !PrintAbove(line) {
c.Println(line)
}
}
}
}
// checkNumbered fails unless out holds line-000 .. line-<n-1> exactly once
// and in order.
func checkNumbered(t *testing.T, run int, out string, n int) {
t.Helper()
got := numbered(out)
for i := 0; i < n; i++ {
want := fmt.Sprintf("line-%03d", i)
if i >= len(got) {
t.Fatalf("run %d: %d of %d lines printed, %s missing:\n%s", run, len(got), n, want, ansi.Strip(out))
}
if got[i] != want {
t.Fatalf("run %d: line %d is %s, want %s (lines: %v)", run, i, got[i], want, got)
}
}
if len(got) != n {
t.Fatalf("run %d: %d lines printed, want %d: %v", run, len(got), n, got)
}
}
// TestPrintAboveOrderedNoLoss (CR-01): every line printed while a spinner,
// progress or process view runs appears once and in order; a line printed
// from another goroutine while the view finishes is not lost.
func TestPrintAboveOrderedNoLoss(t *testing.T) {
const runs, lines = 20, 100
newTTY := func(out io.Writer) *console {
return newConsole(strings.NewReader(""), out, io.Discard, noEnv, boolPtr(true), boolPtr(true))
}
t.Run("spinner", func(t *testing.T) {
for run := range runs {
out := &lockedBuffer{}
c := newTTY(out)
if err := c.Spinner("working", func() error {
printNumbered(c, 0, lines)
return nil
}); err != nil {
t.Fatal(err)
}
checkNumbered(t, run, out.String(), lines)
}
})
t.Run("progress", func(t *testing.T) {
for run := range runs {
out := &lockedBuffer{}
c := newTTY(out)
if err := c.Progress(lines, func(p Progress) error {
printNumbered(c, 0, 40)
Failed(p, "item-a", errors.New("first failure"))
printNumbered(c, 40, 80)
Failed(p, "item-b", errors.New("second failure"))
printNumbered(c, 80, lines)
return nil
}); err != nil {
t.Fatal(err)
}
got := out.String()
checkNumbered(t, run, got, lines)
for _, want := range []string{"item-a: first failure", "item-b: second failure"} {
if !strings.Contains(ansi.Strip(got), want) {
t.Fatalf("run %d: failure line %q missing:\n%s", run, want, ansi.Strip(got))
}
}
}
})
t.Run("process", func(t *testing.T) {
items := []string{"a", "b", "c", "d"}
for run := range runs {
out := &lockedBuffer{}
c := newTTY(out)
next := 0
err := Process(context.Background(), c, "Pushing", items, func(context.Context, string) error {
printNumbered(c, next, next+lines/len(items))
next += lines / len(items)
return nil
})
if err != nil {
t.Fatal(err)
}
checkNumbered(t, run, out.String(), lines)
}
})
t.Run("concurrent_finish", func(t *testing.T) {
const bg = 200
for run := range runs {
out := &lockedBuffer{}
c := newTTY(out)
started := make(chan struct{})
done := make(chan struct{})
if err := c.Spinner("working", func() error {
go func() {
defer close(done)
for i := range bg {
line := fmt.Sprintf("bg-%03d", i)
if !PrintAbove(line) {
_, _ = io.WriteString(out, line+"\n")
}
if i == 5 {
close(started)
}
}
}()
<-started
return nil
}); err != nil {
t.Fatal(err)
}
<-done
count := map[string]int{}
for _, l := range numbered(out.String()) {
count[l]++
}
for i := range bg {
l := fmt.Sprintf("bg-%03d", i)
if count[l] != 1 {
t.Fatalf("run %d: %s printed %d times:\n%s", run, l, count[l], ansi.Strip(out.String()))
}
}
}
})
}
// TestPrintlnStaysWithItsConsole (WR-02): while a view runs on console A,
// another console's Println, Printf and Info go to its own writer; A's own
// Println still prints above its view.
func TestPrintlnStaysWithItsConsole(t *testing.T) {
bufA, bufB := &lockedBuffer{}, &lockedBuffer{}
a := newConsole(strings.NewReader(""), bufA, io.Discard, noEnv, boolPtr(true), boolPtr(true))
b := newConsole(strings.NewReader(""), bufB, bufB, noEnv, boolPtr(false), boolPtr(false))
err := a.Spinner("working", func() error {
b.Println("b-println")
b.Printf("b-printf %d\n", 1)
b.Info("b-info")
a.Println("a-println")
return nil
})
if err != nil {
t.Fatal(err)
}
gotA, gotB := ansi.Strip(bufA.String()), ansi.Strip(bufB.String())
for _, want := range []string{"b-println", "b-printf 1", "b-info"} {
if !strings.Contains(gotB, want) {
t.Errorf("console B's writer lacks %q:\n%s", want, gotB)
}
if strings.Contains(gotA, want) {
t.Errorf("console B's %q printed above console A's view:\n%s", want, gotA)
}
}
if !strings.Contains(gotA, "a-println") {
t.Errorf("console A's own line missing above its view:\n%s", gotA)
}
}
// TestLiveViewSingleOwner (WR-02): two consoles starting a spinner at the
// same moment never both run a live view; the one that loses prints its
// plain line to its own writer.
func TestLiveViewSingleOwner(t *testing.T) {
const rounds = 200
for round := range rounds {
bufs := [2]*lockedBuffer{{}, {}}
var start, entered sync.WaitGroup
start.Add(1)
entered.Add(2)
var wg sync.WaitGroup
for i := range 2 {
wg.Add(1)
go func() {
defer wg.Done()
c := newConsole(strings.NewReader(""), bufs[i], io.Discard, noEnv, boolPtr(true), boolPtr(true))
start.Wait()
_ = c.Spinner(fmt.Sprintf("label-%d", i), func() error {
entered.Done()
entered.Wait() // both work functions run at once
return nil
})
}()
}
start.Done()
wg.Wait()
plain := 0
for i, b := range bufs {
if strings.Contains(ansi.Strip(b.String()), fmt.Sprintf("[...] label-%d", i)) {
plain++
}
}
if plain != 1 {
t.Fatalf("round %d: %d consoles printed their plain line to their own writer, want 1:\nA: %q\nB: %q",
round, plain, ansi.Strip(bufs[0].String()), ansi.Strip(bufs[1].String()))
}
if PrintAbove("after the round") {
t.Fatalf("round %d: a view is still registered", round)
}
}
}
// TestLiveViewPanicUnregisters (WR-02): a panic in the work function still
// ends and unregisters the view; the next widget runs as a live view.
func TestLiveViewPanicUnregisters(t *testing.T) {
out := &lockedBuffer{}
c := newConsole(strings.NewReader(""), out, io.Discard, noEnv, boolPtr(true), boolPtr(true))
func() {
defer func() {
if r := recover(); r != "boom" {
t.Fatalf("recovered %v, want the work function's panic", r)
}
}()
_ = c.Spinner("panicking", func() error { panic("boom") })
}()
if PrintAbove("after the panic") {
t.Fatal("the panicked view is still registered")
}
next := &lockedBuffer{}
c2 := newConsole(strings.NewReader(""), next, io.Discard, noEnv, boolPtr(true), boolPtr(true))
var during bool
if err := c2.Spinner("next", func() error {
during = PrintAbove("above the next view")
return nil
}); err != nil {
t.Fatal(err)
}
if !during || strings.Contains(ansi.Strip(next.String()), "[...] next") {
t.Errorf("the next spinner did not run as a live view:\n%s", ansi.Strip(next.String()))
}
}
// TestProcessViewStripsControl (WR-05): an item name and an item error
// carrying control sequences render without them.
func TestProcessViewStripsControl(t *testing.T) {
name := "evil\x1b]0;pwned\x07item"
var m tea.Model = newProcessModel(DefaultPalette(), "Pushing", []string{name, "next"})
m, _ = m.Update(tea.WindowSizeMsg{Width: 120, Height: 24})
m, _ = m.Update(stepMsg{item: name, state: itemFailed, err: errors.New("rejected \x1b[2J by the remote"), done: 1})
m, _ = m.Update(stepMsg{item: "next", state: itemRunning, done: 1})
out := m.View().Content
if bad := carriesInjected(out); bad != "" {
t.Errorf("process view carries %q:\n%q", bad, out)
}
for _, want := range []string{"evil", "item", "rejected", "by the remote"} {
if !strings.Contains(ansi.Strip(out), want) {
t.Errorf("process view lacks %q:\n%q", want, out)
}
}
}

View File

@@ -128,10 +128,11 @@ func (c *console) setGlobals(g Globals) {
// live reports whether widgets animate: stdout is a terminal and none of
// -q, --no-ansi and -n is set (D-03). A live view that is already running
// makes nested widgets print plain lines.
// makes nested widgets print plain lines: runLive then reports that it did
// not start.
func (c *console) live() bool {
g := c.globals
return c.tty && !g.Quiet && g.Color != ColorNever && !g.NoInteraction && !liveActive()
return c.tty && !g.Quiet && g.Color != ColorNever && !g.NoInteraction
}
// canPrompt reports whether prompts may wait for input from a person:
@@ -170,13 +171,14 @@ func (c *console) styled(w io.Writer) io.Writer {
return &colorprofile.Writer{Forward: w, Profile: c.profile()}
}
// emit writes s, a styled line or block, to w. While a live view runs on
// stdout, lines for stdout print above it instead of tearing it.
// emit writes s, a styled line or block, to w. While a live view this
// console started runs on stdout, lines for stdout print above it instead
// of tearing it.
func (c *console) emit(w io.Writer, s string) {
if w == nil {
return
}
if w == c.out && printAboveLines(s) {
if w == c.out && printAbove(c, s) {
return
}
_, _ = io.WriteString(c.styled(w), s)
@@ -194,7 +196,7 @@ func (c *console) Println(a ...any) {
return
}
s := fmt.Sprintln(a...)
if printAboveLines(s) {
if printAbove(c, s) {
return
}
_, _ = io.WriteString(c.out, s)
@@ -205,7 +207,7 @@ func (c *console) Printf(format string, a ...any) {
return
}
s := fmt.Sprintf(format, a...)
if strings.HasSuffix(s, "\n") && printAboveLines(s) {
if strings.HasSuffix(s, "\n") && printAbove(c, s) {
return
}
_, _ = io.WriteString(c.out, s)

View File

@@ -0,0 +1,105 @@
package bonfire
import (
"bytes"
"context"
"errors"
"strings"
"testing"
)
func newTestRoot(commands []Command, opts ...RootOption) error {
var buf bytes.Buffer
_, err := NewRootIO("acme", commands, strings.NewReader(""), &buf, &buf, opts...)
return err
}
// TestReservedFlags: a command flag that reuses a global option's name or
// shorthand fails NewRoot with an error naming the command and the flag,
// without a panic; Call refuses it the same way.
func TestReservedFlags(t *testing.T) {
for _, name := range []string{"verbose", "quiet", "ansi", "no-ansi", "no-interaction", "env", "help"} {
cmd := Command{Name: "acme:loud", Flags: []Flag{{Name: name}}}
err := newTestRoot([]Command{cmd})
if !errors.Is(err, ErrReservedFlag) || !strings.Contains(err.Error(), `"acme:loud"`) || !strings.Contains(err.Error(), "--"+name) {
t.Errorf("--%s: err = %v", name, err)
}
if err := Call(context.Background(), []Command{cmd}, "acme:loud", nil, nil); !errors.Is(err, ErrReservedFlag) {
t.Errorf("Call with --%s: err = %v", name, err)
}
}
for _, short := range []string{"v", "q", "n", "h"} {
cmd := Command{Name: "acme:loud", Flags: []Flag{{Name: "volume", Shorthand: short}}}
err := newTestRoot([]Command{cmd})
if !errors.Is(err, ErrReservedFlag) || !strings.Contains(err.Error(), "-"+short+" for --volume") || !strings.Contains(err.Error(), `"acme:loud"`) {
t.Errorf("-%s: err = %v", short, err)
}
}
// Other names and shorthands are fine, repeatable and bare ones too.
ok := Command{Name: "acme:loud", Flags: []Flag{
{Name: "volume", Shorthand: "V"}, {Name: "environment", Shorthand: "e"},
{Name: "tag", Shorthand: "t", Repeatable: true}, {Name: "force", Shorthand: "f", Bare: true},
}}
if err := newTestRoot([]Command{ok}); err != nil {
t.Errorf("unreserved flags refused: %v", err)
}
}
// TestDuplicateNames: two commands with one name, or a command named list
// or help, fail NewRoot with ErrDuplicateCommand naming it.
func TestDuplicateNames(t *testing.T) {
cases := map[string][]Command{
"acme:sync": {{Name: "acme:sync"}, {Name: "acme:other"}, {Name: "acme:sync"}},
"list": {{Name: "list"}},
"help": {{Name: "acme:x"}, {Name: "help"}},
}
for name, commands := range cases {
err := newTestRoot(commands)
if !errors.Is(err, ErrDuplicateCommand) || !strings.Contains(err.Error(), `"`+name+`"`) {
t.Errorf("%s: err = %v", name, err)
}
}
if err := newTestRoot([]Command{{Name: "acme:list"}, {Name: "list:all"}, {Name: "acme:help"}}); err != nil {
t.Errorf("names that only contain list or help refused: %v", err)
}
if err := newTestRoot([]Command{{Name: "nonamespace"}}); !errors.Is(err, ErrCommandName) {
t.Errorf("a plugin command without a namespace: err = %v", err)
}
}
// TestCallNoGlobalsHook: a root built by Call runs no OnGlobals hook, even
// when called from a command of a root that has one, and the called
// command still sees the globals passed to it.
func TestCallNoGlobalsHook(t *testing.T) {
var hooked []Globals
var inner Globals
target := Command{Name: "acme:inner", Description: "Inner", Run: func(_ context.Context, _ Input, out Output) error {
inner = GlobalsOf(out)
return nil
}}
outer := Command{Name: "acme:outer", Description: "Outer", Run: func(ctx context.Context, _ Input, out Output) error {
return NewCatalog([]Command{target}).Call(ctx, "acme:inner", []string{"-vvv", "--env=other"}, out)
}}
var buf bytes.Buffer
root, err := NewRootIO("acme", []Command{outer, target}, strings.NewReader(""), &buf, &buf,
OnGlobals(func(g Globals) { hooked = append(hooked, g) }))
if err != nil {
t.Fatal(err)
}
if code := Execute(context.Background(), root, []string{"-v", "acme:outer"}); code != ExitOK {
t.Fatalf("exit %d:\n%s", code, buf.String())
}
if len(hooked) != 1 || hooked[0].Verbosity != 1 || hooked[0].Env != "" {
t.Errorf("hooks = %+v, want one call with the outer -v", hooked)
}
if inner.Verbosity != 3 || inner.Env != "other" {
t.Errorf("called command saw %+v, want -vvv --env=other", inner)
}
if err := Call(context.Background(), []Command{target}, "acme:missing", nil, nil); !errors.Is(err, ErrUnknownCommand) {
t.Errorf("Call of a missing command: %v", err)
}
var nilCatalog *Catalog
if nilCatalog.Has("acme:inner") || !errors.Is(nilCatalog.Call(context.Background(), "acme:inner", nil, nil), ErrUnknownCommand) {
t.Error("a nil Catalog has commands")
}
}

View File

@@ -0,0 +1,7 @@
ERROR db:check: connect to 127.0.0.1:5432: connection refused
Caused by:
- connect to 127.0.0.1:5432: connection refused
- connection refused

View File

@@ -0,0 +1,10 @@
ERROR No application found in /home/jane/acme/plugins.
summer make:command needs an application root (summer.yaml).
Try:
cd ..; bin/acme make:command acme.blog:publish
Run with -v for details.

View File

@@ -0,0 +1,8 @@
ERROR Command "migrat" is not defined.
Try:
bin/acme migrate
Run with -v for details.

View File

@@ -0,0 +1,23 @@
Description:
Run the bundled tool with its own arguments
Usage:
tool:exec [args] [options] [--] <args...>
Arguments:
args The tool's arguments
Global options:
-h, --help Display help for the given command
-q, --quiet Do not output any message
-v|vv|vvv Increase verbosity
--ansi|no-ansi Force (or disable) ANSI output
-n, --no-interaction Do not ask any interactive question
--env The environment the command runs under
Help:
Everything after the command name reaches the tool unchanged, global flags included.
Examples:
# The tool's own version
bin/acme tool:exec --version

View File

@@ -0,0 +1,33 @@
Description:
Create a personal API token for a backend admin
Usage:
admin:token:create <login> [options]
Arguments:
login The admin's login
Options:
--scopes=SCOPES Comma-separated scopes [default: "*"]
--expires=EXPIRES Days until expiry [default: 90]
--json Print the token as JSON
-t, --tag=TAG A tag for the token (multiple values allowed)
Global options:
-h, --help Display help for the given command
-q, --quiet Do not output any message
-v|vv|vvv Increase verbosity
--ansi|no-ansi Force (or disable) ANSI output
-n, --no-interaction Do not ask any interactive question
--env The environment the command runs under
Help:
Creates a token for the admin with the given login and prints it once.
Store it in the client right away; it cannot be shown again.
Examples:
# Token for the MCP server, journal only
bin/acme admin:token:create jane --scopes=journal.*
# Script-friendly output
bin/acme admin:token:create jane --json

26
modules/bonfire/testdata/list.golden vendored Normal file
View File

@@ -0,0 +1,26 @@
SummerCMS acme v1.4.0 (framework v0.3.2)
Usage:
bin/acme <command> [options] [arguments]
Options:
-h, --help Display help for the given command
-q, --quiet Do not output any message
-v|vv|vvv Increase verbosity
--ansi|no-ansi Force (or disable) ANSI output
-n, --no-interaction Do not ask any interactive question
--env The environment the command runs under
Available commands:
help Display help for a command
list List commands
migrate Run the database migrations
serve Serve the application
admin
admin:create Create a backend administrator
admin:token:create Create a personal API token for a backend admin
make
make:command Create a new console command
make:model Create a new model and migration
tool
tool:exec Run the bundled tool with its own arguments

View File

@@ -0,0 +1,273 @@
{
"application": {
"name": "acme",
"version": "v1.4.0",
"framework": "v0.3.2"
},
"global_options": [
{
"name": "help",
"shorthand": "h",
"description": "Display help for the given command",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "quiet",
"shorthand": "q",
"description": "Do not output any message",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "verbose",
"shorthand": "v",
"description": "Increase verbosity (-v, -vv or -vvv)",
"default": "",
"bare": true,
"repeatable": true
},
{
"name": "ansi",
"shorthand": "",
"description": "Force ANSI output",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "no-ansi",
"shorthand": "",
"description": "Disable ANSI output",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "no-interaction",
"shorthand": "n",
"description": "Do not ask any interactive question",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "env",
"shorthand": "",
"description": "The environment the command runs under",
"default": "",
"bare": false,
"repeatable": false
}
],
"commands": [
{
"name": "acme:secret",
"description": "Rotate the internal signing secret",
"long": "",
"usage": "acme:secret [options]",
"hidden": true,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "admin:create",
"description": "Create a backend administrator",
"long": "",
"usage": "admin:create [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "admin:token:create",
"description": "Create a personal API token for a backend admin",
"long": "Creates a token for the admin with the given login and prints it once.\nStore it in the client right away; it cannot be shown again.",
"usage": "admin:token:create \u003clogin\u003e [options]",
"hidden": false,
"passthrough": false,
"arguments": [
{
"name": "login",
"description": "The admin's login",
"required": true
}
],
"options": [
{
"name": "scopes",
"shorthand": "",
"description": "Comma-separated scopes",
"default": "*",
"bare": false,
"repeatable": false
},
{
"name": "expires",
"shorthand": "",
"description": "Days until expiry",
"default": "90",
"bare": false,
"repeatable": false
},
{
"name": "json",
"shorthand": "",
"description": "Print the token as JSON",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "tag",
"shorthand": "t",
"description": "A tag for the token",
"default": "",
"bare": false,
"repeatable": true
}
],
"examples": [
{
"command": "admin:token:create jane --scopes=journal.*",
"description": "Token for the MCP server, journal only"
},
{
"command": "admin:token:create jane --json",
"description": "Script-friendly output"
}
]
},
{
"name": "help",
"description": "Display help for a command",
"long": "Prints the description, usage, arguments, options, long help and examples of a command. Without a command name it prints the command list.",
"usage": "help [command_name] [options]",
"hidden": false,
"passthrough": false,
"arguments": [
{
"name": "command_name",
"description": "The command name",
"required": false
}
],
"options": [],
"examples": [
{
"command": "help migrate",
"description": "Help for the migrate command"
}
]
},
{
"name": "list",
"description": "List commands",
"long": "Prints the commands grouped by namespace. --all includes hidden commands; --format=json prints every command with its arguments, options and examples.",
"usage": "list [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [
{
"name": "all",
"shorthand": "",
"description": "Show hidden commands too",
"default": "",
"bare": true,
"repeatable": false
},
{
"name": "format",
"shorthand": "",
"description": "The output format (txt or json)",
"default": "txt",
"bare": false,
"repeatable": false
}
],
"examples": [
{
"command": "list --all",
"description": "Every command, hidden ones included"
},
{
"command": "list --format=json",
"description": "The catalog for scripts and agents"
}
]
},
{
"name": "make:command",
"description": "Create a new console command",
"long": "",
"usage": "make:command [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "make:model",
"description": "Create a new model and migration",
"long": "",
"usage": "make:model [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "migrate",
"description": "Run the database migrations",
"long": "",
"usage": "migrate [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "serve",
"description": "Serve the application",
"long": "",
"usage": "serve [options]",
"hidden": false,
"passthrough": false,
"arguments": [],
"options": [],
"examples": []
},
{
"name": "tool:exec",
"description": "Run the bundled tool with its own arguments",
"long": "Everything after the command name reaches the tool unchanged, global flags included.",
"usage": "tool:exec [args] [options] [--] \u003cargs...\u003e",
"hidden": false,
"passthrough": true,
"arguments": [
{
"name": "args",
"description": "The tool's arguments",
"required": false
}
],
"options": [],
"examples": [
{
"command": "tool:exec --version",
"description": "The tool's own version"
}
]
}
]
}

View File

@@ -0,0 +1,28 @@
SummerCMS acme v1.4.0 (framework v0.3.2)
Usage:
bin/acme <command> [options] [arguments]
Options:
-h, --help Display help for the given command
-q, --quiet Do not output any message
-v|vv|vvv Increase verbosity
--ansi|no-ansi Force (or disable) ANSI output
-n, --no-interaction Do not ask any interactive question
--env The environment the command runs under
Available commands:
help Display help for a command
list List commands
migrate Run the database migrations
serve Serve the application
acme
acme:secret Rotate the internal signing secret
admin
admin:create Create a backend administrator
admin:token:create Create a personal API token for a backend admin
make
make:command Create a new console command
make:model Create a new model and migration
tool
tool:exec Run the bundled tool with its own arguments

View File

@@ -0,0 +1,26 @@
SummerCMS acme v1.4.0 (framework v0.3.2)
Usage:
bin/acme <command> [options] [arguments]
Options:
-h, --help Display help for the given command
-q, --quiet Do not output any message
-v|vv|vvv Increase verbosity
 --ansi|no-ansi Force (or disable) ANSI output
-n, --no-interaction Do not ask any interactive question
 --env The environment the command runs under
Available commands:
help Display help for a command
list List commands
migrate Run the database migrations
serve Serve the application
admin
admin:create Create a backend administrator
admin:token:create Create a personal API token for a backend admin
make
make:command Create a new console command
make:model Create a new model and migration
tool
tool:exec Run the bundled tool with its own arguments

View File

@@ -7,6 +7,9 @@ import (
// frameworkModule is the module path of the SummerCMS framework.
const frameworkModule = "git.golem15.com/golem15/summercms"
// readBuildInfo reads the binary's build information; tests replace it.
var readBuildInfo = debug.ReadBuildInfo
// Version is the version line of a binary: the application's own version and
// the framework version it was built against.
type Version struct {
@@ -26,7 +29,7 @@ type Version struct {
// the version of the git.golem15.com/golem15/summercms dependency, or App
// when the main module is the framework itself. "(devel)" reads as "devel".
func BuildVersion() Version {
info, ok := debug.ReadBuildInfo()
info, ok := readBuildInfo()
if !ok {
return Version{App: "devel", Framework: "devel"}
}

View File

@@ -0,0 +1,75 @@
package bonfire
import (
"runtime/debug"
"testing"
)
// TestVersionHeader: the list header and BuildVersion for a tagged
// framework dependency, a replaced (local) one, a go.work build (devel)
// and the summer tool's own build, read through an injected build info.
func TestVersionHeader(t *testing.T) {
headers := []struct {
v Version
name string
want string
}{
{Version{App: "v1.4.0", Framework: "v0.3.2"}, "acme", "SummerCMS acme v1.4.0 (framework v0.3.2)"},
{Version{App: "devel", Framework: "v0.4.0", Local: true}, "acme", "SummerCMS acme devel (framework v0.4.0, local)"},
{Version{App: "v1.4.1-0.20261021120000-abcdef123456+dirty", Framework: "(devel)"}, "acme", "SummerCMS acme v1.4.1-0.20261021120000-abcdef123456+dirty (framework devel)"},
// Equal versions collapse to one, as for the summer tool's own build.
{Version{App: "(devel)", Framework: "(devel)"}, "acme", "SummerCMS acme devel"},
{Version{App: "v0.4.0", Framework: "v0.4.0"}, "summer", "SummerCMS summer v0.4.0"},
{Version{App: "v0.4.0", Framework: "v0.4.0", Local: true}, "acme", "SummerCMS acme v0.4.0 (framework v0.4.0, local)"},
{Version{}, "acme", "SummerCMS acme devel"},
}
for _, tc := range headers {
if got := tc.v.Header(tc.name); got != tc.want {
t.Errorf("%+v.Header(%q) = %q, want %q", tc.v, tc.name, got, tc.want)
}
}
dep := func(version string, replaced bool) *debug.Module {
m := &debug.Module{Path: frameworkModule, Version: version}
if replaced {
m.Replace = &debug.Module{Path: "../summercms.go", Version: "(devel)"}
}
return m
}
builds := []struct {
name string
info *debug.BuildInfo
ok bool
want Version
}{
{"tagged", &debug.BuildInfo{Main: debug.Module{Path: "example.com/acme", Version: "v1.4.0"},
Deps: []*debug.Module{{Path: "example.com/other", Version: "v9.9.9"}, dep("v0.3.2", false)}},
true, Version{App: "v1.4.0", Framework: "v0.3.2"}},
{"replaced", &debug.BuildInfo{Main: debug.Module{Path: "example.com/acme", Version: "(devel)"},
Deps: []*debug.Module{dep("v0.4.0", true)}},
true, Version{App: "devel", Framework: "v0.4.0", Local: true}},
{"go.work", &debug.BuildInfo{Main: debug.Module{Path: "example.com/acme", Version: "v1.4.1-0.20261021120000-abcdef123456+dirty"},
Deps: []*debug.Module{dep("(devel)", false)}},
true, Version{App: "v1.4.1-0.20261021120000-abcdef123456+dirty", Framework: "devel"}},
{"no framework dep", &debug.BuildInfo{Main: debug.Module{Path: "example.com/acme", Version: "v1.0.0"}},
true, Version{App: "v1.0.0", Framework: "devel"}},
{"summer", &debug.BuildInfo{Main: debug.Module{Path: frameworkModule, Version: "v0.4.0"}},
true, Version{App: "v0.4.0", Framework: "v0.4.0"}},
{"no build info", nil, false, Version{App: "devel", Framework: "devel"}},
}
for _, tc := range builds {
t.Run(tc.name, func(t *testing.T) {
prev := readBuildInfo
t.Cleanup(func() { readBuildInfo = prev })
readBuildInfo = func() (*debug.BuildInfo, bool) { return tc.info, tc.ok }
if got := BuildVersion(); got != tc.want {
t.Errorf("BuildVersion() = %+v, want %+v", got, tc.want)
}
})
}
// The test binary is built from the framework module itself.
if got := BuildVersion(); got.App != got.Framework || got.Local {
t.Errorf("the framework's own build = %+v, want App == Framework", got)
}
}

View File

@@ -26,13 +26,16 @@ func (c *console) Spinner(message string, fn func() error) error {
if c.globals.Quiet {
return fn()
}
if !c.live() {
c.Printf("[...] %s\n", message)
return fn()
if c.live() {
started, err := runLive(context.Background(), c.out, c, newSpinnerModel(c.palette, message), func(func(tea.Msg)) error {
return fn()
})
if started {
return err
}
}
return runLive(context.Background(), c.out, c, newSpinnerModel(c.palette, message), func(func(tea.Msg)) error {
return fn()
})
c.Printf("[...] %s\n", message)
return fn()
}
func (c *console) Progress(total int, fn func(Progress) error) error {
@@ -45,12 +48,15 @@ func (c *console) Progress(total int, fn func(Progress) error) error {
if c.globals.Quiet {
return fn(&quietProgress{c: c})
}
if !c.live() {
return fn(&progressBar{c: c, total: total, lastPct: -1})
if c.live() {
started, err := runLive(context.Background(), c.out, c, newProgressModel(c.palette, total), func(send func(tea.Msg)) error {
return fn(&liveProgress{send: send, total: total})
})
if started {
return err
}
}
return runLive(context.Background(), c.out, c, newProgressModel(c.palette, total), func(send func(tea.Msg)) error {
return fn(&liveProgress{send: send, total: total})
})
return fn(&progressBar{c: c, total: total, lastPct: -1})
}
// progressBar prints a line at every tenth of the work when widgets do not
@@ -90,7 +96,7 @@ func (p *progressBar) Advance(step int) {
func (p *progressBar) Current(string) {}
func (p *progressBar) Fail(item string, err error) {
p.c.Error(failureLine(item, err))
p.c.Error(failureText(item, err))
}
// quietProgress prints nothing but failures (-q).
@@ -103,7 +109,7 @@ func (p *quietProgress) Advance(int) {}
func (p *quietProgress) Current(string) {}
func (p *quietProgress) Fail(item string, err error) {
p.c.Error(failureLine(item, err))
p.c.Error(failureText(item, err))
}
// liveProgress drives the live progress model.
@@ -129,7 +135,7 @@ func (p *liveProgress) Current(item string) {
func (p *liveProgress) Fail(item string, err error) {
p.send(itemMsg{done: p.current, total: p.total, item: item, err: err})
p.send(printAboveMsg(failureLine(item, err)))
p.send(printLine(failureText(item, err)))
}
func failureLine(item string, err error) string {
@@ -139,7 +145,14 @@ func failureLine(item string, err error) string {
return item + ": " + err.Error()
}
// failureText is the failure line as printed: one line without control
// characters.
func failureText(item string, err error) string {
return Sanitize(failureLine(item, err))
}
func (c *console) Table(headers []string, rows [][]string) {
headers = padRow(headers, len(headers))
if !c.tty {
c.Println(strings.Join(headers, "\t"))
for _, row := range rows {
@@ -165,11 +178,13 @@ func (c *console) Table(headers []string, rows [][]string) {
c.emit(c.out, t.String()+"\n")
}
// padRow returns n cells of row (missing cells empty), each passed through
// Sanitize so a cell cannot move the cursor or retitle the terminal.
func padRow(row []string, n int) []string {
out := make([]string, n)
for i := 0; i < n; i++ {
if i < len(row) {
out[i] = row[i]
out[i] = Sanitize(row[i])
}
}
return out

View File

@@ -0,0 +1,320 @@
package bonfire
import (
"bytes"
"context"
"errors"
"strings"
"testing"
"github.com/charmbracelet/x/ansi"
)
// noEnv is a lookup with no environment variables set.
func noEnv(string) string { return "" }
// exerciseWidgets runs every widget once on c: status lines, a spinner, a
// progress bar of four steps, the process view over a, b (failing) and c,
// a table, a data line and an error.
func exerciseWidgets(t *testing.T, c *console) {
t.Helper()
c.Info("info line")
c.Success("success line")
c.Warning("warning line")
if err := c.Spinner("spinning", func() error { return nil }); err != nil {
t.Fatalf("Spinner: %v", err)
}
if err := c.Progress(4, func(p Progress) error {
for range 4 {
p.Advance(1)
}
return nil
}); err != nil {
t.Fatalf("Progress: %v", err)
}
err := Process(context.Background(), c, "Importing", []string{"a", "b", "c"}, func(_ context.Context, item string) error {
if item == "b" {
return errors.New("bad row")
}
return nil
})
if err == nil || !strings.Contains(err.Error(), "b: bad row") {
t.Fatalf("Process err = %v", err)
}
c.Table([]string{"key", "value"}, [][]string{{"k1", "v1"}, {"k2"}})
c.Println("data line")
c.Error("boom")
}
// TestWidgetMatrix: every widget degrades by mode. Not a terminal gives
// the pre-phase plain lines byte for byte; -q keeps data and errors only;
// --no-ansi and -n on a terminal print plain lines (no live view), and
// --no-ansi writes no escape codes; a terminal runs the live views.
func TestWidgetMatrix(t *testing.T) {
type streams struct{ out, err bytes.Buffer }
newC := func(s *streams, tty bool, g Globals) *console {
c := newConsole(strings.NewReader(""), &s.out, &s.err, noEnv, boolPtr(tty), boolPtr(tty))
c.setGlobals(g)
return c
}
t.Run("not a terminal", func(t *testing.T) {
var s streams
exerciseWidgets(t, newC(&s, false, Globals{}))
wantOut := "ℹ info line\n✓ success line\n⚠ warning line\n" +
"[...] spinning\n" +
"[2/4] 50%\n[4/4] 100%\n" +
"✓ a\n✓ c\n" +
"key\tvalue\nk1\tv1\nk2\t\n" +
"data line\n"
if got := s.out.String(); got != wantOut {
t.Errorf("stdout:\n got %q\nwant %q", got, wantOut)
}
if got, want := s.err.String(), "✗ b: bad row\n✗ boom\n"; got != want {
t.Errorf("stderr:\n got %q\nwant %q", got, want)
}
})
t.Run("quiet", func(t *testing.T) {
for _, tty := range []bool{false, true} {
var s streams
exerciseWidgets(t, newC(&s, tty, Globals{Quiet: true, NoInteraction: true}))
out := ansi.Strip(s.out.String())
for _, gone := range []string{"info line", "success line", "warning line", "spinning", "[4/4]", "4/4", "✓ a", "Importing"} {
if strings.Contains(out, gone) {
t.Errorf("tty %v: -q printed %q:\n%s", tty, gone, out)
}
}
for _, kept := range []string{"k1", "v1", "data line\n"} {
if !strings.Contains(out, kept) {
t.Errorf("tty %v: -q dropped data %q:\n%s", tty, kept, out)
}
}
errOut := ansi.Strip(s.err.String())
if !strings.Contains(errOut, "✗ b: bad row") || !strings.Contains(errOut, "✗ boom") {
t.Errorf("tty %v: -q dropped errors: %q", tty, errOut)
}
}
})
t.Run("no-ansi on a terminal", func(t *testing.T) {
var s streams
exerciseWidgets(t, newC(&s, true, Globals{Color: ColorNever}))
out := s.out.String()
if strings.Contains(out, "\x1b") || strings.Contains(s.err.String(), "\x1b") {
t.Errorf("--no-ansi wrote escape codes:\n%q\n%q", out, s.err.String())
}
for _, want := range []string{"ℹ info line\n", "[...] spinning\n", "[2/4] 50%\n", "✓ a\n", "data line\n", "┌"} {
if !strings.Contains(out, want) {
t.Errorf("--no-ansi lacks %q:\n%s", want, out)
}
}
})
t.Run("no-interaction on a terminal", func(t *testing.T) {
var s streams
exerciseWidgets(t, newC(&s, true, Globals{NoInteraction: true}))
out := s.out.String()
if strings.Contains(out, "\x1b[?25l") {
t.Errorf("-n ran a live view (cursor hidden):\n%q", out)
}
plain := ansi.Strip(out)
for _, want := range []string{"ℹ info line\n", "[...] spinning\n", "[4/4] 100%\n", "✓ a\n", "data line\n"} {
if !strings.Contains(plain, want) {
t.Errorf("-n lacks %q:\n%s", want, plain)
}
}
if !strings.Contains(out, "\x1b[") {
t.Errorf("-n on a color terminal lost its colors:\n%q", out)
}
})
t.Run("terminal", func(t *testing.T) {
var s streams
c := newC(&s, true, Globals{})
if !c.live() {
t.Fatal("a terminal console without flags does not animate")
}
exerciseWidgets(t, c)
// The live views ran as bubbletea programs (the renderer hides the
// cursor). A buffer has no window size, so the renderer draws no
// frames here; TestLiveModels renders them with a fixed size.
if n := strings.Count(s.out.String(), "\x1b[?25l"); n != 3 {
t.Errorf("want 3 live views (spinner, progress, process), cursor hidden %d times:\n%q", n, s.out.String())
}
out := ansi.Strip(s.out.String())
for _, want := range []string{"ℹ info line", "┌", "k1", "data line"} {
if !strings.Contains(out, want) {
t.Errorf("terminal output lacks %q:\n%s", want, out)
}
}
if strings.Contains(out, "[...] spinning") || strings.Contains(out, "[2/4] 50%") || strings.Contains(out, "✓ a\n") {
t.Errorf("terminal printed the plain fallback lines:\n%s", out)
}
if !strings.Contains(ansi.Strip(s.err.String()), "✗ boom") {
t.Errorf("terminal error line missing: %q", s.err.String())
}
})
}
// TestPromptsNoInteraction: with -n or -q every prompt returns its default
// without reading stdin or printing, even on an interactive terminal.
func TestPromptsNoInteraction(t *testing.T) {
for _, g := range []Globals{{NoInteraction: true}, {Quiet: true, NoInteraction: true}} {
in := strings.NewReader("typed\n")
var buf bytes.Buffer
c := newConsole(in, &buf, &buf, noEnv, boolPtr(true), boolPtr(true))
c.setGlobals(g)
if got, err := c.Ask("name", "bob"); err != nil || got != "bob" {
t.Errorf("%+v: Ask = %q, %v", g, got, err)
}
for _, def := range []bool{true, false} {
if got, err := c.Confirm("sure?", def); err != nil || got != def {
t.Errorf("%+v: Confirm(def %v) = %v, %v", g, def, got, err)
}
}
choices := []string{"a", "b", "c"}
for def, want := range map[int]string{0: "a", 2: "c", 5: "a", -1: "a"} {
if got, err := c.Choice("pick", choices, def); err != nil || got != want {
t.Errorf("%+v: Choice(def %d) = %q, %v; want %q", g, def, got, err, want)
}
}
if _, err := c.Choice("pick", nil, 0); err == nil {
t.Errorf("%+v: Choice without options did not fail", g)
}
if in.Len() != len("typed\n") || buf.Len() != 0 {
t.Errorf("%+v: prompts read stdin or printed: left %d, out %q", g, in.Len(), buf.String())
}
if Interactive(c) {
t.Errorf("%+v: Interactive is true", g)
}
}
}
// TestSecretWithNoInteraction: under -n a secret is read from piped stdin
// (empty on EOF); with a terminal stdin it fails with a hint naming the
// piped form, because a secret has no default.
func TestSecretWithNoInteraction(t *testing.T) {
var buf bytes.Buffer
c := newConsole(strings.NewReader(" s3cret-value \nnext\n"), &buf, &buf, noEnv, boolPtr(false), boolPtr(false))
c.setGlobals(Globals{NoInteraction: true})
if got, err := c.Secret("password"); err != nil || got != "s3cret-value" {
t.Fatalf("piped Secret = %q, %v", got, err)
}
if buf.Len() != 0 {
t.Errorf("piped Secret printed a prompt: %q", buf.String())
}
empty := newConsole(strings.NewReader(""), &buf, &buf, noEnv, boolPtr(false), boolPtr(false))
empty.setGlobals(Globals{NoInteraction: true})
if got, err := empty.Secret("password"); err != nil || got != "" {
t.Errorf("Secret on empty stdin = %q, %v", got, err)
}
term := newConsole(strings.NewReader("never read\n"), &buf, &buf, noEnv, boolPtr(true), boolPtr(true))
term.setGlobals(Globals{NoInteraction: true})
term.inTTY = true
_, err := term.Secret("password")
var hint *HintError
if !errors.As(err, &hint) || !errors.Is(err, errSecretNoInteraction) {
t.Fatalf("terminal Secret under -n: %v", err)
}
var block bytes.Buffer
RenderError(&block, "acme", err, Globals{Color: ColorNever})
if !strings.Contains(block.String(), `printf '%s\n' "$VALUE" | bin/acme <command> -n`) {
t.Errorf("hint does not show the piped form:\n%s", block.String())
}
}
// TestConfirmProceed: off a terminal, under -n and for an Output that is
// not a bonfire console it proceeds without reading stdin; on an
// interactive terminal it asks, defaulting to no.
func TestConfirmProceed(t *testing.T) {
in := strings.NewReader("n\n")
var buf bytes.Buffer
for name, c := range map[string]*console{
"not a terminal": newConsole(in, &buf, &buf, noEnv, boolPtr(false), boolPtr(false)),
"-n": newConsole(in, &buf, &buf, noEnv, boolPtr(true), boolPtr(true)),
} {
if name == "-n" {
c.setGlobals(Globals{NoInteraction: true})
}
if ok, err := ConfirmProceed(c, "Drop every table?"); err != nil || !ok {
t.Errorf("%s: ConfirmProceed = %v, %v", name, ok, err)
}
}
if in.Len() != 2 || buf.Len() != 0 {
t.Errorf("ConfirmProceed read stdin or printed: left %d, out %q", in.Len(), buf.String())
}
if ok, err := ConfirmProceed(struct{ Output }{}, "Drop?"); err != nil || !ok {
t.Errorf("foreign Output: ConfirmProceed = %v, %v", ok, err)
}
for answer, want := range map[string]bool{"n\n": false, "y\n": true, "yes\n": true, "\n": false, "": false} {
var out bytes.Buffer
c := newConsole(strings.NewReader(answer), &out, &out, noEnv, boolPtr(true), boolPtr(true))
// --no-ansi keeps the line prompt on a terminal (huh needs a real
// terminal).
c.setGlobals(Globals{Color: ColorNever})
ok, err := ConfirmProceed(c, "Drop every table?")
if err != nil || ok != want {
t.Errorf("answer %q: ConfirmProceed = %v, %v; want %v", answer, ok, err, want)
}
if !strings.Contains(out.String(), "Drop every table? [y/N]") {
t.Errorf("answer %q: question not asked: %q", answer, out.String())
}
}
}
// TestPrintAbove: without a live view PrintAbove reports false; while one
// runs, the line (and Println output) prints above it.
func TestPrintAbove(t *testing.T) {
if PrintAbove("nobody listens") {
t.Fatal("PrintAbove reported a running view")
}
var out, errW bytes.Buffer
c := newConsole(strings.NewReader(""), &out, &errW, noEnv, boolPtr(true), boolPtr(true))
var during bool
err := c.Spinner("working", func() error {
during = PrintAbove("log line above the view")
c.Println("println above the view")
return nil
})
if err != nil || !during {
t.Fatalf("Spinner err %v, PrintAbove during the view %v", err, during)
}
if PrintAbove("after") {
t.Error("PrintAbove reported a view after it ended")
}
got := ansi.Strip(out.String())
for _, want := range []string{"log line above the view", "println above the view"} {
if !strings.Contains(got, want) {
t.Errorf("output lacks %q:\n%s", want, got)
}
}
if strings.Contains(got, "[...] working") {
t.Errorf("the spinner did not run as a live view:\n%s", got)
}
}
// TestTableStripsControl (WR-05): table headers and cells carrying control
// sequences print without them, boxed with color on a terminal and as
// tab-separated lines off one.
func TestTableStripsControl(t *testing.T) {
headers := []string{"Name\x1b]0;pwned\x07", "Scope"}
rows := [][]string{{"token \x1b[2J one", "journal\x07.posts"}}
for _, tty := range []bool{true, false} {
var out bytes.Buffer
c := newConsole(strings.NewReader(""), &out, &out, noEnv, boolPtr(tty), boolPtr(tty))
c.setGlobals(Globals{Color: ColorAlways})
c.Table(headers, rows)
got := out.String()
if bad := carriesInjected(got); bad != "" {
t.Errorf("terminal %v: table carries %q:\n%q", tty, bad, got)
}
for _, want := range []string{"Name", "token", "one", "journal", "posts"} {
if !strings.Contains(got, want) {
t.Errorf("terminal %v: table lacks %q:\n%q", tty, want, got)
}
}
}
}

View File

@@ -0,0 +1,134 @@
package cabana_test
import (
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/party"
)
// tokenJSONApp is a command app on the package database with the demo
// plugin's scopes and one administrator.
func tokenJSONApp(t *testing.T, login string) *backpack.App {
t.Helper()
gdb := adminGorm(t)
if err := gdb.AutoMigrate(&tokenDemoWidget{}); err != nil {
t.Fatal(err)
}
app := commandApp(t, gdb)
if err := app.Publish(party.Activated{tokenDemoPlugin{fsys: tokenDemoFS()}}); err != nil {
t.Fatal(err)
}
insertAdmin(t, gdb, login, login+"@example.test", "secret-pass", true, false)
return app
}
// callToken runs one admin:token command through bonfire.Call, so the
// global options and flags are parsed as on the command line.
func callToken(t *testing.T, app *backpack.App, name string, args ...string) string {
t.Helper()
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), cabana.RuntimeCommands(app), name, args, &buf); err != nil {
t.Fatalf("%s %v: %v\n%s", name, args, err, buf.String())
}
return buf.String()
}
// TestTokenCreateJSON: admin:token:create --json prints one JSON object
// with id, name, token, scopes and expires_at, under -q too.
func TestTokenCreateJSON(t *testing.T) {
app := tokenJSONApp(t, "json-create")
for _, extra := range [][]string{nil, {"-q"}} {
args := append([]string{"json-create", "--name", "agent", "--scope", "demo", "--expires", "2027-01-01", "--json"}, extra...)
out := callToken(t, app, "admin:token:create", args...)
var got struct {
ID uint `json:"id"`
Name string `json:"name"`
Token string `json:"token"`
Scopes []string `json:"scopes"`
ExpiresAt *string `json:"expires_at"`
}
dec := json.NewDecoder(strings.NewReader(out))
dec.DisallowUnknownFields()
if err := dec.Decode(&got); err != nil {
t.Fatalf("%v: not the JSON object: %v\n%s", extra, err, out)
}
if got.ID == 0 || got.Name != "agent" || !wellFormedPlain(got.Token) || strings.Join(got.Scopes, ",") != "demo" ||
got.ExpiresAt == nil || !strings.HasPrefix(*got.ExpiresAt, "2027-01-01") {
t.Errorf("%v: created = %+v", extra, got)
}
}
}
// TestTokenListJSON: admin:token:list --json prints a JSON array of every
// active token with its hint and never the token itself; --revoked adds
// revoked ones; an empty list is [].
func TestTokenListJSON(t *testing.T) {
app := tokenJSONApp(t, "json-list")
empty := callToken(t, app, "admin:token:list", "json-list", "--json")
if strings.TrimSpace(empty) != "[]" {
t.Fatalf("empty list = %q, want []", empty)
}
plain := strings.TrimSpace(callToken(t, app, "admin:token:create", "json-list", "--name", "one", "--scope", "demo"))
second := strings.TrimSpace(callToken(t, app, "admin:token:create", "json-list", "--name", "two", "--scope", "demo"))
out := callToken(t, app, "admin:token:list", "json-list", "--json")
var list []map[string]any
if err := json.Unmarshal([]byte(out), &list); err != nil {
t.Fatalf("not a JSON array: %v\n%s", err, out)
}
if len(list) != 2 {
t.Fatalf("list = %v", list)
}
for _, key := range []string{"id", "login", "name", "scopes", "hint", "created_at", "last_used_at", "expires_at", "revoked_at"} {
if _, ok := list[0][key]; !ok {
t.Errorf("token entry lacks %s: %v", key, list[0])
}
}
if list[0]["login"] != "json-list" || list[0]["name"] != "two" || list[0]["hint"] != "g15_…"+second[len(second)-4:] {
t.Errorf("newest entry = %v", list[0])
}
if strings.Contains(out, plain) || strings.Contains(out, second) {
t.Fatalf("the list leaks a plaintext token:\n%s", out)
}
id := uint64(list[1]["id"].(float64))
callToken(t, app, "admin:token:revoke", jsonNumber(id))
if active := callToken(t, app, "admin:token:list", "json-list", "--json"); strings.Count(active, `"id":`) != 1 {
t.Errorf("revoked token still listed:\n%s", active)
}
if all := callToken(t, app, "admin:token:list", "json-list", "--json", "--revoked", "-q"); strings.Count(all, `"id":`) != 2 || !strings.Contains(all, `"revoked_at": "`) {
t.Errorf("--revoked -q list:\n%s", all)
}
}
// TestTokenCreateQuietKeepsToken: under -q admin:token:create still prints
// the token, and only the token (data, not decoration).
func TestTokenCreateQuietKeepsToken(t *testing.T) {
app := tokenJSONApp(t, "json-quiet")
out := callToken(t, app, "admin:token:create", "json-quiet", "--name", "agent", "--scope", "demo", "-q")
if strings.Count(out, "\n") != 1 || !wellFormedPlain(strings.TrimSpace(out)) {
t.Fatalf("-q output = %q, want one token line", out)
}
var list []struct {
ID uint64 `json:"id"`
}
if err := json.Unmarshal([]byte(callToken(t, app, "admin:token:list", "json-quiet", "--json")), &list); err != nil || len(list) != 1 {
t.Fatalf("list: %v %v", list, err)
}
revoked := callToken(t, app, "admin:token:revoke", jsonNumber(list[0].ID), "-q")
if revoked != "" {
t.Errorf("-q revoke printed %q, want nothing", revoked)
}
}
func jsonNumber(n uint64) string {
b, _ := json.Marshal(n)
return string(b)
}

View File

@@ -0,0 +1,174 @@
//go:build linux
package conga
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"syscall"
"testing"
"time"
"unsafe"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/compass"
)
// ptyOutput collects what the program writes to a pseudo-terminal.
type ptyOutput struct {
mu sync.Mutex
buf strings.Builder
}
func (p *ptyOutput) String() string {
p.mu.Lock()
defer p.mu.Unlock()
return p.buf.String()
}
// openPTY opens a pseudo-terminal pair: the slave is a terminal for the
// console (stdin and stdout), input written to the master reaches the
// slave's stdin, and everything the slave prints is collected.
func openPTY(t *testing.T) (master, slave *os.File, output *ptyOutput) {
t.Helper()
m, err := os.OpenFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
t.Fatalf("open /dev/ptmx: %v", err)
}
var unlock int32
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, m.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&unlock))); e != 0 {
t.Fatalf("unlock pty: %v", e)
}
var n uint32
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, m.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); e != 0 {
t.Fatalf("pty number: %v", e)
}
s, err := os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
t.Fatalf("open pty slave: %v", err)
}
output = &ptyOutput{}
done := make(chan struct{})
go func() {
defer close(done)
buf := make([]byte, 4096)
for {
n, err := m.Read(buf)
output.mu.Lock()
output.buf.Write(buf[:n])
output.mu.Unlock()
if err != nil {
return
}
}
}()
t.Cleanup(func() {
_ = s.Close()
_ = m.Close()
<-done
})
return m, s, output
}
// productionApp is an app in the production environment without a DSN.
func productionApp(t *testing.T, env string) *backpack.App {
t.Helper()
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Env: env, Environ: []string{"SUMMER_ENV=" + env}})
if err != nil {
t.Fatal(err)
}
return backpack.New(cfg)
}
// runQueueClear runs queue:clear with args on stdin and stdout of the
// caller's choosing and returns the exit code and the error stream.
func runQueueClear(t *testing.T, app *backpack.App, in io.Reader, out, errW io.Writer, args ...string) int {
t.Helper()
var clear bonfire.Command
for _, c := range RuntimeCommands(app, nil) {
if c.Name == "queue:clear" {
clear = c
}
}
if clear.Run == nil {
t.Fatal("queue:clear not registered")
}
root, err := bonfire.NewRootIO("acme", []bonfire.Command{clear}, in, out, errW)
if err != nil {
t.Fatal(err)
}
return bonfire.Execute(context.Background(), root, append([]string{"queue:clear"}, args...))
}
// TestQueueClearConfirm: in production queue:clear asks on an interactive
// terminal and a "no" ends it with exit 1 and no error block, before the
// database is opened; --force, -n, a non-production environment and a
// non-terminal stdin skip the question (the run then reaches the database,
// which these apps do not configure).
func TestQueueClearConfirm(t *testing.T) {
prod := productionApp(t, "production")
master, slave, output := openPTY(t)
if _, err := master.Write([]byte("n\n")); err != nil {
t.Fatal(err)
}
if code := runQueueClear(t, prod, slave, slave, slave, "--no-ansi", "imports"); code != bonfire.ExitFailure {
t.Fatalf("answer no: exit %d\n%s", code, output.String())
}
waitFor(t, output, "Cancelled.")
got := output.String()
if !strings.Contains(got, `Delete every pending job of queue "imports" in production? [y/N]`) {
t.Errorf("question not asked:\n%s", got)
}
if strings.Contains(got, "ERROR") || strings.Contains(got, "database.dsn") {
t.Errorf("a cancelled run printed an error block or reached the database:\n%s", got)
}
for name, tc := range map[string]struct {
app *backpack.App
args []string
}{
"--force": {prod, []string{"--no-ansi", "--force"}},
"-n": {prod, []string{"--no-ansi", "-n"}},
"not production": {productionApp(t, "staging"), []string{"--no-ansi"}},
} {
master, slave, output := openPTY(t)
if _, err := master.Write([]byte("n\n")); err != nil {
t.Fatal(err)
}
if code := runQueueClear(t, tc.app, slave, slave, slave, tc.args...); code != bonfire.ExitFailure {
t.Fatalf("%s: exit %d", name, code)
}
waitFor(t, output, "database.dsn")
if strings.Contains(output.String(), "[y/N]") {
t.Errorf("%s: the question was asked:\n%s", name, output.String())
}
}
// Off a terminal: no question, stdin is never read.
in := strings.NewReader("n\n")
var out, errW strings.Builder
if code := runQueueClear(t, prod, in, &out, &errW); code != bonfire.ExitFailure {
t.Fatalf("non-terminal: exit %d", code)
}
if in.Len() != 2 || strings.Contains(out.String(), "[y/N]") || !strings.Contains(errW.String(), "database.dsn") {
t.Errorf("non-terminal run asked or read stdin (left %d):\n%s\n%s", in.Len(), out.String(), errW.String())
}
}
// waitFor waits until the pty output contains want.
func waitFor(t *testing.T, output *ptyOutput, want string) {
t.Helper()
for range 200 {
if strings.Contains(output.String(), want) {
return
}
<-time.After(10 * time.Millisecond)
}
t.Fatalf("pty output lacks %q:\n%s", want, output.String())
}

View File

@@ -0,0 +1,91 @@
package conga
import (
"bytes"
"log/slog"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/diary"
)
// TestRiverLoggerComponent: River logs as component river, which the
// framework defaults hold at warn while conga's own lines (component
// conga) follow log.level; verbosity 1 lets River's info through.
func TestRiverLoggerComponent(t *testing.T) {
var buf bytes.Buffer
t.Cleanup(diary.Reset)
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Environ: []string{"SUMMER_LOG__DEFAULT=console"}})
if err != nil {
t.Fatal(err)
}
if err := diary.Setup(cfg, diary.Options{Stderr: &buf, Color: bonfire.ColorNever, Root: t.TempDir(), Environ: []string{}}); err != nil {
t.Fatal(err)
}
riverLogger(nil).Info("river info at v0")
riverLogger(nil).Warn("river warn at v0")
loggerFromApp(nil).Info("conga info at v0")
out := buf.String()
if strings.Contains(out, "river info at v0") {
t.Errorf("River info passed its warn floor:\n%s", out)
}
for _, want := range []string{"component=river", "river warn at v0", "component=conga", "conga info at v0"} {
if !strings.Contains(out, want) {
t.Errorf("output lacks %q:\n%s", want, out)
}
}
for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
if strings.Contains(line, "river warn at v0") && !strings.Contains(line, "component=river") {
t.Errorf("River line without component=river: %q", line)
}
if strings.Contains(line, "conga info at v0") && !strings.Contains(line, "component=conga") {
t.Errorf("conga line without component=conga: %q", line)
}
}
buf.Reset()
diary.SetVerbosity(1, false)
riverLogger(nil).Info("river info at v1")
if !strings.Contains(buf.String(), "river info at v1") {
t.Errorf("River info missing at verbosity 1:\n%s", buf.String())
}
}
// TestRiverLoggerPublishedOverride: a *slog.Logger the application
// published wins for River and for conga's own lines; without one, an app
// still gets the diary loggers.
func TestRiverLoggerPublishedOverride(t *testing.T) {
var buf bytes.Buffer
t.Cleanup(diary.Reset)
diary.Bootstrap(&buf, diary.Options{Color: bonfire.ColorNever})
bare := backpack.New(&compass.Config{})
riverLogger(bare).Warn("bare app river")
loggerFromApp(bare).Warn("bare app conga")
if !strings.Contains(buf.String(), "bare app river") || !strings.Contains(buf.String(), "bare app conga") {
t.Fatalf("an app without a published logger does not use diary:\n%s", buf.String())
}
var capture bytes.Buffer
app := backpack.New(&compass.Config{})
published := slog.New(slog.NewTextHandler(&capture, &slog.HandlerOptions{Level: slog.LevelDebug}))
if err := app.Publish(published); err != nil {
t.Fatal(err)
}
buf.Reset()
if riverLogger(app) != published || loggerFromApp(app) != published {
t.Fatal("the published logger is not the one handed out")
}
riverLogger(app).Info("published river info")
loggerFromApp(app).Info("published conga info")
if !strings.Contains(capture.String(), "published river info") || !strings.Contains(capture.String(), "published conga info") {
t.Errorf("published logger lacks the records:\n%s", capture.String())
}
if buf.Len() != 0 {
t.Errorf("records reached diary although the app published a logger:\n%s", buf.String())
}
}

View File

@@ -0,0 +1,87 @@
package conga
import (
"context"
"errors"
"io"
"testing"
"git.golem15.com/golem15/summercms/modules/diary"
"github.com/riverqueue/river/rivertype"
)
// TestWorkerStats: runAttempt counts every attempt of the running worker:
// done on nil, failed on an error or a panic, in flight while it runs, and
// handed back when Stop cancelled it; a nil worker has no counters.
func TestWorkerStats(t *testing.T) {
// The panicking attempt logs its stack; keep it out of the test output.
t.Cleanup(diary.Reset)
diary.Bootstrap(io.Discard, diary.Options{})
m := &Manager{}
counters := &workerCounters{}
m.counters.Store(counters)
w := &Worker{counters: counters}
row := &rivertype.JobRow{ID: 1, Kind: "acme_probe", Attempt: 1, MaxAttempts: 3}
ctx := context.Background()
for range 3 {
if err := m.runAttempt(ctx, row, func(context.Context) error { return nil }); err != nil {
t.Fatal(err)
}
}
boom := errors.New("boom")
if err := m.runAttempt(ctx, row, func(context.Context) error { return boom }); !errors.Is(err, boom) {
t.Fatalf("failing attempt returned %v", err)
}
if err := m.runAttempt(ctx, row, func(context.Context) error { panic("kaboom") }); err == nil {
t.Fatal("a panic did not become an error")
}
started, release := make(chan struct{}), make(chan struct{})
done := make(chan error, 1)
go func() {
done <- m.runAttempt(ctx, row, func(context.Context) error {
close(started)
<-release
return nil
})
}()
<-started
if got := w.Stats(); got.InFlight != 1 || got.Done != 3 || got.Failed != 2 {
t.Fatalf("while running: %+v, want 1 in flight, 3 done, 2 failed", got)
}
close(release)
if err := <-done; err != nil {
t.Fatal(err)
}
// Stop cancels the running attempts: they are handed back, not failed.
counters.stopping.Store(true)
cctx, cancel := context.WithCancel(ctx)
cancel()
if err := m.runAttempt(cctx, row, func(c context.Context) error { return c.Err() }); err == nil {
t.Fatal("cancelled attempt returned nil")
}
// An error unrelated to the cancellation while stopping still fails.
if err := m.runAttempt(ctx, row, func(context.Context) error { return boom }); err == nil {
t.Fatal("failing attempt returned nil")
}
want := WorkerStats{Done: 4, Failed: 3, InFlight: 0, HandedBack: 1}
if got := w.Stats(); got != want {
t.Fatalf("Stats() = %+v, want %+v", got, want)
}
var none *Worker
if got := none.Stats(); got != (WorkerStats{}) {
t.Fatalf("nil worker Stats() = %+v", got)
}
if got := (&Worker{}).Stats(); got != (WorkerStats{}) {
t.Fatalf("worker without counters Stats() = %+v", got)
}
// Without a running worker, attempts run and nothing is counted.
idle := &Manager{}
if err := idle.runAttempt(ctx, row, func(context.Context) error { return nil }); err != nil {
t.Fatal(err)
}
}

View File

@@ -30,7 +30,7 @@ Use cases:
- Environment overrides: `SUMMER_LOG__` variables overlay the defaults key by key, and string values parse where YAML gives numbers or lists (`SUMMER_LOG__CHANNELS__DAILY__DAYS=30`, `SUMMER_LOG__CHANNELS__STACK__CHANNELS=console,daily`).
- Validation: an unknown driver, format or level, a stack naming a missing channel or containing itself, and a relative daily path that leaves the application root fail `diary.Setup` with an error naming the key.
- Console lines in bonfire's palette: time, level, the component as a fixed-width prefix, the message and `key=value` attributes. Control characters other than tab are removed from messages and values, so logged data cannot inject terminal escape sequences. While a bonfire live view (spinner, progress bar, process view) runs, styled lines print above it through `bonfire.PrintAbove`.
- Daily files: the path `storage/logs/summer.log` gives files `storage/logs/summer-2026-10-21.log`, created mode 0640 in directories created 0750, opened on the first record, rotated at local midnight, and deleted after the retention in days (only files of that name pattern). A relative path resolves against the application root. A directory that cannot be written logs one warning from component `diary` on the console and disables the channel; it never fails the start-up.
- Daily files: the path `storage/logs/summer.log` gives files `storage/logs/summer-2026-10-21.log`, created mode 0640 in directories created 0750, opened on the first record, rotated at local midnight, and deleted after the retention in days (only files of that name pattern). A relative path resolves against the application root. A file that cannot be opened (a directory that cannot be written, a full disk) logs a warning from component `diary` on the console, once per day; the channel skips records and tries again a minute later or at the first record of the next day, and it never fails the start-up.
- Redaction: every channel leaf is wrapped by `sunscreen.Wrap`.
- Runtime verbosity: `diary.SetVerbosity` lets info (1) or debug (2) through for every component and adds source locations on styled lines (3); quiet raises only the console channels to warn.
- Console global options: `diary.FromGlobals` turns `-v`/`-vv`/`-vvv`, `-q` and `--ansi`/`--no-ansi` into `diary.Options`, and `diary.ApplyGlobals` applies them to the running setup (the generated main registers it with `bonfire.OnGlobals`).

View File

@@ -0,0 +1,202 @@
package diary_test
import (
"log/slog"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/diary"
)
// TestConfigRules: every invalid value in config/log.yaml fails with an
// error naming its exact key; YAML lists and comma strings parse alike.
func TestConfigRules(t *testing.T) {
cases := []struct {
name string
yaml string
key string
}{
{"level", "level: loud\n", "log.level"},
{"default undefined", "default: nowhere\n", "log.default"},
{"default empty", "default: \"\"\n", "log.default"},
{"driver", "channels:\n daily:\n driver: syslog\n", "log.channels.daily.driver"},
{"driver missing", "channels:\n extra:\n level: info\n", "log.channels.extra.driver"},
{"format", "channels:\n console:\n format: xml\n", "log.channels.console.format"},
{"auto outside console", "channels:\n stderr:\n format: auto\n", "log.channels.stderr.format"},
{"channel level", "channels:\n daily:\n level: chatty\n", "log.channels.daily.level"},
{"days negative", "channels:\n daily:\n days: -3\n", "log.channels.daily.days"},
{"days fraction", "channels:\n daily:\n days: 1.5\n", "log.channels.daily.days"},
{"days text", "channels:\n daily:\n days: often\n", "log.channels.daily.days"},
{"unknown setting", "channels:\n daily:\n rotate: hourly\n", "log.channels.daily.rotate"},
{"channel not a map", "channels:\n daily: yes\n", "log.channels.daily"},
{"daily path missing", "channels:\n daily:\n path: \"\"\n", "log.channels.daily.path"},
{"path escapes", "channels:\n daily:\n path: ../outside/summer.log\n", "log.channels.daily.path"},
{"path escapes after clean", "channels:\n daily:\n path: storage/../../outside.log\n", "log.channels.daily.path"},
{"stack empty", "channels:\n stack:\n channels: []\n", "log.channels.stack.channels"},
{"stack child", "channels:\n stack:\n channels: [console, nowhere]\n", "log.channels.stack.channels"},
{"stack self", "channels:\n stack:\n channels: [console, stack]\n", "log.channels.stack.channels"},
{"stack cycle", "default: a\nchannels:\n a:\n driver: stack\n channels: [console, b]\n b:\n driver: stack\n channels: [a]\n", "log.channels.a.channels"},
{"component level", "components:\n river: chatty\n", "log.components.river"},
{"component nested", "components:\n golem15:\n blog: info\n", "log.components.golem15"},
{"components not a map", "components: warn\n", "log.components"},
{"channels not a map", "channels: console\n", "log.channels"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := diary.ParseConfig(openYAMLConfig(t, tc.yaml))
if err == nil {
t.Fatalf("ParseConfig accepted:\n%s", tc.yaml)
}
if !strings.Contains(err.Error(), tc.key) {
t.Errorf("error %q does not name %s", err, tc.key)
}
})
}
// A cycle error names the path it found.
_, err := diary.ParseConfig(openYAMLConfig(t, cases[19].yaml))
if err == nil || !strings.Contains(err.Error(), "a -> b -> a") {
t.Errorf("cycle error = %v, want the path a -> b -> a", err)
}
// A YAML list, a comma string in YAML and a comma string from the
// environment give the same stack.
list, err := diary.ParseConfig(openYAMLConfig(t, "channels:\n stack:\n channels: [console, daily]\n"))
if err != nil {
t.Fatal(err)
}
comma, err := diary.ParseConfig(openYAMLConfig(t, "channels:\n stack:\n channels: \" console , daily \"\n"))
if err != nil {
t.Fatal(err)
}
env, err := diary.ParseConfig(openConfig(t, "SUMMER_LOG__CHANNELS__STACK__CHANNELS=console,daily"))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(list, comma) || !reflect.DeepEqual(list, env) {
t.Errorf("list, comma and env stacks differ:\n list %+v\ncomma %+v\n env %+v", list.Channels["stack"], comma.Channels["stack"], env.Channels["stack"])
}
// Levels accept warning as warn and any case; days 0 keeps every file.
got, err := diary.ParseConfig(openYAMLConfig(t, "level: WARNING\nchannels:\n daily:\n days: 0\n"))
if err != nil {
t.Fatal(err)
}
if got.Level != slog.LevelWarn || got.Channels["daily"].Days != 0 {
t.Errorf("level %v days %d, want WARN and 0", got.Level, got.Channels["daily"].Days)
}
// compass keeps a dotted YAML key literal, so it does not nest; only
// the nested form (above, or from SUMMER_LOG__ variables) is refused.
got, err = diary.ParseConfig(openYAMLConfig(t, "components:\n golem15.blog: debug\n"))
if err != nil {
t.Fatal(err)
}
if got.Components["golem15.blog"] != slog.LevelDebug {
t.Errorf("literal dotted component = %v", got.Components)
}
}
// TestConfigEnvStrings: SUMMER_LOG__ overrides arrive as strings and
// overlay the defaults key by key, also on top of config/log.yaml.
func TestConfigEnvStrings(t *testing.T) {
got, err := diary.ParseConfig(openConfig(t,
"SUMMER_LOG__LEVEL=debug",
"SUMMER_LOG__CHANNELS__DAILY__LEVEL=error",
"SUMMER_LOG__CHANNELS__DAILY__DAYS=7",
"SUMMER_LOG__COMPONENTS__JOURNAL=debug",
))
if err != nil {
t.Fatalf("ParseConfig: %v", err)
}
want := diary.DefaultConfig()
want.Level = slog.LevelDebug
daily := want.Channels["daily"]
daily.Level, daily.HasLevel, daily.Days = slog.LevelError, true, 7
want.Channels["daily"] = daily
want.Components["journal"] = slog.LevelDebug
if !reflect.DeepEqual(got, want) {
t.Errorf("env overlay:\n got %+v\nwant %+v", got, want)
}
// The environment wins over the file, and keys the environment does
// not set keep the file's values.
got, err = diary.ParseConfig(openYAMLConfig(t,
"channels:\n daily:\n path: var/log/app.log\n days: 3\n",
"SUMMER_LOG__CHANNELS__DAILY__DAYS=9",
))
if err != nil {
t.Fatal(err)
}
if d := got.Channels["daily"]; d.Path != "var/log/app.log" || d.Days != 9 || d.Format != diary.FormatJSON || d.Level != slog.LevelWarn {
t.Errorf("daily channel = %+v, want path var/log/app.log, days 9 and the default format and level", d)
}
// A nil config gives the defaults.
none, err := diary.ParseConfig(nil)
if err != nil || !reflect.DeepEqual(none, diary.DefaultConfig()) {
t.Errorf("ParseConfig(nil) = %+v, %v; want the defaults", none, err)
}
}
// TestDefaultConfig: the defaults equal the D-08 example (components river
// warn), and the config/log.yaml that summer new writes parses to the
// defaults plus its http component.
func TestDefaultConfig(t *testing.T) {
example := `default: stack
level: info
channels:
stack:
driver: stack
channels: [console, daily]
console:
driver: console
format: auto
daily:
driver: daily
path: storage/logs/summer.log
days: 14
format: json
level: warn
components:
river: warn
`
got, err := diary.ParseConfig(openYAMLConfig(t, example))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, diary.DefaultConfig()) {
t.Errorf("D-08 example:\n got %+v\nwant %+v", got, diary.DefaultConfig())
}
def := diary.DefaultConfig()
if def.Default != "stack" || def.Level != slog.LevelInfo || def.Components["river"] != slog.LevelWarn || len(def.Components) != 1 {
t.Errorf("DefaultConfig head = %q %v %v", def.Default, def.Level, def.Components)
}
for _, name := range []string{"stack", "console", "daily", "stderr", "null"} {
if _, ok := def.Channels[name]; !ok {
t.Errorf("DefaultConfig lacks channel %q", name)
}
}
// The defaults are a fresh copy each time.
def.Components["river"] = slog.LevelDebug
if diary.DefaultConfig().Components["river"] != slog.LevelWarn {
t.Error("DefaultConfig returns shared maps")
}
generated, err := os.ReadFile(filepath.Join("..", "..", "internal", "build", "testdata", "new", "config__log.yaml.golden"))
if err != nil {
t.Fatal(err)
}
got, err = diary.ParseConfig(openYAMLConfig(t, string(generated)))
if err != nil {
t.Fatalf("generated config/log.yaml: %v", err)
}
want := diary.DefaultConfig()
want.Components["http"] = slog.LevelInfo
if !reflect.DeepEqual(got, want) {
t.Errorf("generated config/log.yaml:\n got %+v\nwant %+v", got, want)
}
}

View File

@@ -12,7 +12,6 @@ import (
"sync"
"sync/atomic"
"time"
"unicode"
"charm.land/lipgloss/v2"
"git.golem15.com/golem15/summercms/modules/bonfire"
@@ -141,12 +140,12 @@ func (h *consoleHandler) Handle(ctx context.Context, r slog.Record) error {
label, style := levelLabel(r.Level, p)
b.WriteString(style.Render(label))
b.WriteString(strings.Repeat(" ", max(0, 5-len(label))+1))
name := sanitize(component)
name := bonfire.Sanitize(component)
if name != "" {
b.WriteString(p.Header.Render(name))
}
b.WriteString(strings.Repeat(" ", max(0, componentWidth-len(name))+1))
b.WriteString(sanitize(r.Message))
b.WriteString(bonfire.Sanitize(r.Message))
for _, list := range [][]consoleAttr{h.attrs, recAttrs} {
for _, a := range list {
b.WriteByte(' ')
@@ -247,7 +246,7 @@ func appendAttr(out []consoleAttr, prefix string, a slog.Attr) []consoleAttr {
}
return out
}
return append(out, consoleAttr{key: sanitize(prefix + a.Key), value: formatValue(a.Value)})
return append(out, consoleAttr{key: bonfire.Sanitize(prefix + a.Key), value: formatValue(a.Value)})
}
func formatValue(v slog.Value) string {
@@ -266,35 +265,9 @@ func formatValue(v slog.Value) string {
default:
s = v.String()
}
s = sanitize(s)
s = bonfire.Sanitize(s)
if s == "" || strings.ContainsAny(s, " \t\"=") {
return strconv.Quote(s)
}
return s
}
// sanitize removes control characters other than tab; line breaks become
// spaces so a multi-line value stays on one line.
func sanitize(s string) string {
clean := true
for _, r := range s {
if r != '\t' && unicode.IsControl(r) {
clean = false
break
}
}
if clean {
return s
}
var b strings.Builder
for _, r := range s {
switch {
case r == '\n' || r == '\r':
b.WriteByte(' ')
case r != '\t' && unicode.IsControl(r):
default:
b.WriteRune(r)
}
}
return b.String()
}

View File

@@ -0,0 +1,159 @@
package diary_test
import (
"bytes"
"context"
"log/slog"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/diary"
"github.com/charmbracelet/colorprofile"
)
// record handles one record with a zero time (no time column) on h.
func record(t *testing.T, h slog.Handler, level slog.Level, msg string, attrs ...slog.Attr) {
t.Helper()
r := slog.NewRecord(time.Time{}, level, msg, 0)
r.AddAttrs(attrs...)
if err := h.Handle(context.Background(), r); err != nil {
t.Fatalf("Handle: %v", err)
}
}
// TestConsoleStripsControlChars: control characters in the message, in
// attribute keys and in values never reach the terminal; line breaks become
// spaces, so one record is one line.
func TestConsoleStripsControlChars(t *testing.T) {
for _, profile := range []colorprofile.Profile{colorprofile.NoTTY, colorprofile.ANSI256} {
var buf bytes.Buffer
h := diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: profile})
record(t, h, slog.LevelInfo, "evil \x1b[31mred\x1b[0m\a bell\nsecond line\rreturn",
slog.String("value", "a\x1b]0;title\x07b\nc"),
slog.String("k\x1bey", "v"),
slog.Any("err", errString("boom\x1b[2Jclear")))
out := buf.String()
if strings.Count(out, "\n") != 1 || !strings.HasSuffix(out, "\n") {
t.Errorf("profile %v: record is not exactly one line: %q", profile, out)
}
for _, bad := range []string{"\x1b[31mred", "\x1b]0;", "\a", "\x07", "\r", "\x1b[2J", "k\x1bey"} {
if strings.Contains(out, bad) {
t.Errorf("profile %v: output carries %q: %q", profile, bad, out)
}
}
for _, want := range []string{"[31mred", "bell second line return", "[2Jclear", "key="} {
if !strings.Contains(out, want) {
t.Errorf("profile %v: output lacks the cleaned text %q: %q", profile, want, out)
}
}
}
}
type errString string
func (e errString) Error() string { return string(e) }
// TestConsoleComponentPrefix: the component is a fixed-width column, so
// messages line up whether a record has a short component, none or one
// given as a record attribute; inside a group it is an ordinary attribute.
func TestConsoleComponentPrefix(t *testing.T) {
var buf bytes.Buffer
h := diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: colorprofile.NoTTY})
record(t, h.WithAttrs([]slog.Attr{slog.String(diary.ComponentKey, "db")}), slog.LevelInfo, "MSG")
record(t, h.WithAttrs([]slog.Attr{slog.String(diary.ComponentKey, "journal")}), slog.LevelWarn, "MSG")
record(t, h, slog.LevelError, "MSG")
record(t, h, slog.LevelDebug, "MSG", slog.String(diary.ComponentKey, "river"))
record(t, h.WithGroup("g").WithAttrs([]slog.Attr{slog.String(diary.ComponentKey, "inner")}), slog.LevelInfo, "MSG")
lines := strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n")
if len(lines) != 5 {
t.Fatalf("got %d lines, want 5:\n%s", len(lines), buf.String())
}
wantPrefix := []string{
"INFO db MSG",
"WARN journal MSG",
"ERROR MSG",
"DEBUG river MSG",
"INFO MSG g.component=inner",
}
for i, want := range wantPrefix {
if !strings.HasPrefix(lines[i], want) {
t.Errorf("line %d = %q, want prefix %q", i, lines[i], want)
}
if col := strings.Index(lines[i], "MSG"); col != 17 {
t.Errorf("line %d: message at column %d, want 17: %q", i, col, lines[i])
}
}
if strings.Contains(lines[3], "component=") {
t.Errorf("record-level component printed as an attribute: %q", lines[3])
}
// A record with a time starts with HH:MM:SS.
buf.Reset()
r := slog.NewRecord(time.Date(2026, 10, 21, 9, 5, 7, 0, time.Local), slog.LevelInfo, "timed", 0)
if err := h.Handle(context.Background(), r); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(buf.String(), "09:05:07 INFO ") {
t.Errorf("timed line = %q, want the 09:05:07 prefix", buf.String())
}
}
// TestConsoleStyledOnlyOnTerminal: the NoTTY profile writes no escape
// codes and ANSI256 does; through Setup, the auto format styles only when
// the profile allows it (never on a plain buffer unless forced).
func TestConsoleStyledOnlyOnTerminal(t *testing.T) {
for _, tc := range []struct {
profile colorprofile.Profile
styled bool
}{{colorprofile.NoTTY, false}, {colorprofile.ANSI256, true}, {colorprofile.TrueColor, true}} {
var buf bytes.Buffer
h := diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: tc.profile})
record(t, h.WithAttrs([]slog.Attr{slog.String(diary.ComponentKey, "blog")}), slog.LevelWarn, "styled?", slog.Int("n", 1))
if got := strings.Contains(buf.String(), "\x1b["); got != tc.styled {
t.Errorf("profile %v: escape codes %v, want %v: %q", tc.profile, got, tc.styled, buf.String())
}
}
for _, tc := range []struct {
name string
color bonfire.ColorMode
environ []string
styled bool
}{
{"auto on a buffer", bonfire.ColorAuto, []string{}, false},
{"auto forced", bonfire.ColorAuto, []string{"FORCE_COLOR=1"}, true},
{"auto forced but NO_COLOR", bonfire.ColorAuto, []string{"FORCE_COLOR=1", "NO_COLOR=1"}, false},
{"always", bonfire.ColorAlways, []string{}, true},
{"never forced", bonfire.ColorNever, []string{"FORCE_COLOR=1"}, false},
} {
t.Run(tc.name, func(t *testing.T) {
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf, Color: tc.color, Environ: tc.environ}, "SUMMER_LOG__DEFAULT=console")
diary.For("blog").Info("hello")
out := buf.String()
if got := strings.Contains(out, "\x1b["); got != tc.styled {
t.Errorf("escape codes %v, want %v: %q", got, tc.styled, out)
}
if !tc.styled && !strings.Contains(out, "level=INFO") {
t.Errorf("unstyled console is not logfmt: %q", out)
}
})
}
}
// TestConsoleLevelFromOptions: ConsoleOptions.Level gates records and nil
// means info.
func TestConsoleLevelFromOptions(t *testing.T) {
var buf bytes.Buffer
h := diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: colorprofile.NoTTY})
if h.Enabled(context.Background(), slog.LevelDebug) || !h.Enabled(context.Background(), slog.LevelInfo) {
t.Error("default console level is not info")
}
h = diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: colorprofile.NoTTY, Level: slog.LevelError})
if h.Enabled(context.Background(), slog.LevelWarn) {
t.Error("console level error lets warn through")
}
}

View File

@@ -12,23 +12,38 @@ import (
const dateLayout = "2006-01-02"
// dailyRetry is how long a daily writer skips records after a file failed
// to open before it tries again on the same date.
const dailyRetry = time.Minute
// dailyWriter writes to one file per local date: path storage/logs/summer.log
// gives storage/logs/summer-2026-10-21.log. Files open lazily on the first
// write (no empty files), are created 0o640 in directories created 0o750,
// and on every open the files of the same base older than the retention are
// deleted. If a file cannot be opened the writer reports once through warn
// and is disabled: later writes succeed without writing anything.
// deleted. If a file cannot be opened the writer skips records (writes
// still succeed) and tries again one minute later by its clock, or at the
// first record of a new date, whichever comes first; it reports through
// warn once per date that fails.
type dailyWriter struct {
mu sync.Mutex
dir string
base string
ext string
days int
now func() time.Time
warn func(path string, err error)
file *os.File
date string
disabled bool
mu sync.Mutex
dir string
base string
ext string
days int
now func() time.Time
warn func(path string, err error)
file *os.File
date string
// failedDate is the date whose file failed to open last; no open is
// tried for it before retryAt.
failedDate string
retryAt time.Time
// warnedDate is the last date a failure was reported for.
warnedDate string
// retired is set when a new setup replaced the writer's channels: a
// late record is still appended, but the file is closed again after
// every write, so the writer keeps no descriptor.
retired bool
}
func newDailyWriter(path string, days int, now func() time.Time, warn func(string, error)) *dailyWriter {
@@ -54,15 +69,19 @@ func (d *dailyWriter) fileName(date string) string {
// Write appends p to the current day's file in one write call.
func (d *dailyWriter) Write(p []byte) (int, error) {
d.mu.Lock()
if d.disabled {
d.mu.Unlock()
return len(p), nil
}
date := d.now().Local().Format(dateLayout)
now := d.now()
date := now.Local().Format(dateLayout)
if d.file == nil || date != d.date {
if date == d.failedDate && now.Before(d.retryAt) {
d.mu.Unlock()
return len(p), nil
}
if err := d.open(date); err != nil {
d.disabled = true
warn := d.warn
d.failedDate, d.retryAt = date, now.Add(dailyRetry)
var warn func(string, error)
if d.warnedDate != date {
d.warnedDate, warn = date, d.warn
}
name := d.fileName(date)
d.mu.Unlock()
if warn != nil {
@@ -70,8 +89,13 @@ func (d *dailyWriter) Write(p []byte) (int, error) {
}
return len(p), nil
}
d.failedDate = ""
}
n, err := d.file.Write(p)
if d.retired {
_ = d.file.Close()
d.file, d.date = nil, ""
}
d.mu.Unlock()
return n, err
}
@@ -139,6 +163,16 @@ func (d *dailyWriter) prune(today string) {
}
}
// retire closes the open file for good: later writes, from goroutines that
// still hold the replaced channels, open the day's file, append and close
// it again.
func (d *dailyWriter) retire() error {
d.mu.Lock()
d.retired = true
d.mu.Unlock()
return d.Close()
}
// Close closes the open file. A later write opens it again.
func (d *dailyWriter) Close() error {
d.mu.Lock()

347
modules/diary/daily_test.go Normal file
View File

@@ -0,0 +1,347 @@
//go:build unix
package diary_test
import (
"os"
"path/filepath"
"slices"
"strings"
"syscall"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/diary"
)
// dailyOnly is the environment of a configuration whose default channel is
// the daily file at info.
var dailyOnly = []string{"SUMMER_LOG__DEFAULT=daily", "SUMMER_LOG__CHANNELS__DAILY__LEVEL=info"}
// TestDailyRotation: a record before and one after local midnight land in
// two files named by their dates.
func TestDailyRotation(t *testing.T) {
root := t.TempDir()
clk := newClock(day(21, 23, 59, 59))
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf, Root: root, Now: clk.Now}, dailyOnly...)
blog := diary.For("blog")
blog.Info("before midnight")
clk.Set(day(22, 0, 0, 1))
blog.Info("after midnight")
dir := filepath.Join(root, "storage", "logs")
first := readDaily(t, filepath.Join(dir, "summer-2026-10-21.log"))
second := readDaily(t, filepath.Join(dir, "summer-2026-10-22.log"))
if len(first) != 1 || !strings.Contains(first[0], "before midnight") {
t.Errorf("2026-10-21 file = %q, want only the record before midnight", first)
}
if len(second) != 1 || !strings.Contains(second[0], "after midnight") {
t.Errorf("2026-10-22 file = %q, want only the record after midnight", second)
}
if buf.String() != "" {
t.Errorf("console written although the default channel is daily:\n%s", buf.String())
}
}
// TestDailyRetention: with days 2 the files of today and the day before are
// kept, older files of the same base are deleted, and every other file in
// the directory is left alone. days 0 keeps everything.
func TestDailyRetention(t *testing.T) {
keep := []string{
"summer-2026-10-20.log", "summer-2026-10-21.log",
"summer.log", "other-2026-10-01.log", "summer-2026-10-01.json",
"summer-latest.log", "summer-2026-1-1.log", "notes.txt", "xsummer-2026-10-01.log",
}
gone := []string{"summer-2026-10-19.log", "summer-2026-10-18.log", "summer-2025-01-01.log"}
prepare := func(t *testing.T) string {
t.Helper()
root := t.TempDir()
dir := filepath.Join(root, "storage", "logs")
if err := os.MkdirAll(filepath.Join(dir, "summer-2026-10-01.log"), 0o750); err != nil {
t.Fatal(err)
}
for _, name := range append(slices.Clone(keep), gone...) {
if err := os.WriteFile(filepath.Join(dir, name), []byte("old\n"), 0o640); err != nil {
t.Fatal(err)
}
}
return root
}
list := func(t *testing.T, root string) []string {
t.Helper()
entries, err := os.ReadDir(filepath.Join(root, "storage", "logs"))
if err != nil {
t.Fatal(err)
}
var names []string
for _, e := range entries {
names = append(names, e.Name())
}
return names
}
root := prepare(t)
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__DAYS=2")...)
diary.For("blog").Info("today")
names := list(t, root)
for _, name := range append(slices.Clone(keep), "summer-2026-10-01.log") {
if !slices.Contains(names, name) {
t.Errorf("retention deleted %s; left %v", name, names)
}
}
for _, name := range gone {
if slices.Contains(names, name) {
t.Errorf("retention kept %s; left %v", name, names)
}
}
today := readDaily(t, filepath.Join(root, "storage", "logs", "summer-2026-10-21.log"))
if len(today) != 2 || today[0] != "old" || !strings.Contains(today[1], `"msg":"today"`) {
t.Errorf("today's file = %q, want the old line and the new record appended", today)
}
diary.Reset()
root = prepare(t)
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__DAYS=0")...)
diary.For("blog").Info("today")
names = list(t, root)
for _, name := range append(slices.Clone(keep), gone...) {
if !slices.Contains(names, name) {
t.Errorf("days 0 deleted %s", name)
}
}
}
// TestDailyPermissions: a new daily file is 0640 whatever the umask, its
// directory 0750 under the usual umask, and an existing file keeps its mode.
func TestDailyPermissions(t *testing.T) {
old := syscall.Umask(0o022)
t.Cleanup(func() { syscall.Umask(old) })
root := t.TempDir()
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}}, dailyOnly...)
diary.For("blog").Info("first")
dir := filepath.Join(root, "storage", "logs")
file := filepath.Join(dir, "summer-2026-10-21.log")
readDaily(t, file)
assertMode(t, file, 0o640)
assertMode(t, dir, 0o750)
// A strict umask cannot strip the file's group read bit.
diary.Reset()
syscall.Umask(0o077)
root = t.TempDir()
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}}, dailyOnly...)
diary.For("blog").Info("strict umask")
file = filepath.Join(root, "storage", "logs", "summer-2026-10-21.log")
readDaily(t, file)
assertMode(t, file, 0o640)
// An existing file is appended to and keeps its mode.
diary.Reset()
syscall.Umask(0o022)
root = t.TempDir()
dir = filepath.Join(root, "storage", "logs")
if err := os.MkdirAll(dir, 0o750); err != nil {
t.Fatal(err)
}
file = filepath.Join(dir, "summer-2026-10-21.log")
if err := os.WriteFile(file, nil, 0o600); err != nil {
t.Fatal(err)
}
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}}, dailyOnly...)
diary.For("blog").Info("existing")
if lines := readDaily(t, file); len(lines) != 1 {
t.Errorf("existing file has %d lines, want 1", len(lines))
}
assertMode(t, file, 0o600)
}
func assertMode(t *testing.T, path string, want os.FileMode) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if got := info.Mode().Perm(); got != want {
t.Errorf("%s mode = %o, want %o", path, got, want)
}
}
// TestDailyUnwritable: a daily file that cannot be opened warns once (on a
// plain stderr handler when no console channel exists), never fails Setup,
// and later records are dropped silently.
func TestDailyUnwritable(t *testing.T) {
root := t.TempDir()
// A regular file where the log directory should be: MkdirAll fails for
// every user, root included.
if err := os.WriteFile(filepath.Join(root, "blocker"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
var buf lockedBuffer
setup(t, diary.Options{Root: root, Stderr: &buf},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__PATH=blocker/logs/summer.log")...)
blog := diary.For("blog")
for i := range 5 {
blog.Info("record", "i", i)
}
out := buf.String()
if n := strings.Count(out, "daily log file disabled"); n != 1 {
t.Fatalf("warning appears %d times, want 1:\n%s", n, out)
}
for _, want := range []string{"level=WARN", "component=diary", filepath.Join(root, "blocker", "logs", "summer-2026-10-21.log")} {
if !strings.Contains(out, want) {
t.Errorf("warning lacks %q:\n%s", want, out)
}
}
if strings.Contains(out, "msg=record") {
t.Errorf("records reached stderr although the default channel is daily:\n%s", out)
}
if err := diary.Close(); err != nil {
t.Errorf("Close after a disabled daily file: %v", err)
}
}
// TestDailyLazyOpen: no file or directory exists until a record reaches the
// daily channel; a closed file opens again on the next record.
func TestDailyLazyOpen(t *testing.T) {
root := t.TempDir()
var buf lockedBuffer
setup(t, diary.Options{Root: root, Stderr: &buf})
storage := filepath.Join(root, "storage")
if _, err := os.Stat(storage); !os.IsNotExist(err) {
t.Fatalf("Setup created %s (err %v)", storage, err)
}
diary.For("blog").Info("below the daily level")
if _, err := os.Stat(storage); !os.IsNotExist(err) {
t.Fatalf("an info record created %s although the daily level is warn (err %v)", storage, err)
}
if !strings.Contains(buf.String(), "below the daily level") {
t.Errorf("console lacks the info record:\n%s", buf.String())
}
file := filepath.Join(storage, "logs", "summer-2026-10-21.log")
diary.For("blog").Warn("first warning")
if lines := readDaily(t, file); len(lines) != 1 {
t.Fatalf("daily file has %d lines after one warning, want 1", len(lines))
}
diary.For("blog").Warn("after close")
if lines := readDaily(t, file); len(lines) != 2 {
t.Fatalf("daily file has %d lines after reopening, want 2", len(lines))
}
}
// TestPathConfinement: a relative daily path may not leave the application
// root; a path that stays inside after cleaning and an absolute path are
// accepted, and relative paths resolve against Options.Root.
func TestPathConfinement(t *testing.T) {
for _, path := range []string{"../summer.log", "storage/../../summer.log", "./../logs/summer.log", ".."} {
_, err := diary.ParseConfig(openConfig(t, "SUMMER_LOG__CHANNELS__DAILY__PATH="+path))
if err == nil || !strings.Contains(err.Error(), "log.channels.daily.path") {
t.Errorf("path %q: err %v, want an error naming log.channels.daily.path", path, err)
}
}
abs := filepath.Join(t.TempDir(), "elsewhere", "app.log")
for _, path := range []string{"storage/../logs/summer.log", "logs/app.log", abs} {
if _, err := diary.ParseConfig(openConfig(t, "SUMMER_LOG__CHANNELS__DAILY__PATH="+path)); err != nil {
t.Errorf("path %q rejected: %v", path, err)
}
}
root := t.TempDir()
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__PATH="+abs)...)
diary.For("blog").Info("absolute")
if lines := readDaily(t, filepath.Join(filepath.Dir(abs), "app-2026-10-21.log")); len(lines) != 1 {
t.Errorf("absolute path file has %d lines, want 1", len(lines))
}
diary.Reset()
setup(t, diary.Options{Root: root, Stderr: &lockedBuffer{}},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__PATH=storage/../logs/app.log")...)
diary.For("blog").Info("relative")
if lines := readDaily(t, filepath.Join(root, "logs", "app-2026-10-21.log")); len(lines) != 1 {
t.Errorf("relative path did not resolve against the root: %d lines", len(lines))
}
}
// TestDailyRetriesAfterFailure (WR-04): a daily file that failed to open is
// retried one minute later by the writer's clock, or on the first record of
// a new date, whichever comes first; the warning appears once per date.
func TestDailyRetriesAfterFailure(t *testing.T) {
start := func(t *testing.T, at time.Time) (root string, clk *clock, buf *lockedBuffer) {
t.Helper()
root = t.TempDir()
blocker := filepath.Join(root, "blocker")
if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
clk = newClock(at)
buf = &lockedBuffer{}
setup(t, diary.Options{Root: root, Stderr: buf, Now: clk.Now},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__PATH=blocker/logs/summer.log")...)
return root, clk, buf
}
warnings := func(buf *lockedBuffer) int { return strings.Count(buf.String(), "daily log file disabled") }
blog := diary.For("blog")
t.Run("one minute later", func(t *testing.T) {
root, clk, buf := start(t, day(21, 10, 0, 0))
blog.Info("first")
if n := warnings(buf); n != 1 {
t.Fatalf("%d warnings after the failed open, want 1:\n%s", n, buf.String())
}
if err := os.Remove(filepath.Join(root, "blocker")); err != nil {
t.Fatal(err)
}
file := filepath.Join(root, "blocker", "logs", "summer-2026-10-21.log")
clk.Set(day(21, 10, 0, 30))
blog.Info("within the minute")
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatalf("retried before the minute passed (stat err %v)", err)
}
if n := warnings(buf); n != 1 {
t.Fatalf("%d warnings within the minute, want 1:\n%s", n, buf.String())
}
clk.Set(day(21, 10, 1, 1))
blog.Info("after the minute")
lines := readDaily(t, file)
if len(lines) != 1 || !strings.Contains(lines[0], "after the minute") {
t.Fatalf("daily file after the retry = %q, want the record after the minute", lines)
}
})
t.Run("next date", func(t *testing.T) {
root, clk, buf := start(t, day(21, 23, 59, 59))
blog.Info("before midnight")
if err := os.Remove(filepath.Join(root, "blocker")); err != nil {
t.Fatal(err)
}
clk.Set(day(22, 0, 0, 1))
blog.Info("after midnight")
lines := readDaily(t, filepath.Join(root, "blocker", "logs", "summer-2026-10-22.log"))
if len(lines) != 1 || !strings.Contains(lines[0], "after midnight") {
t.Fatalf("daily file of the new date = %q", lines)
}
if n := warnings(buf); n != 1 {
t.Errorf("%d warnings, want 1:\n%s", n, buf.String())
}
})
t.Run("next date fails too", func(t *testing.T) {
_, clk, buf := start(t, day(21, 23, 59, 59))
blog.Info("before midnight")
clk.Set(day(22, 0, 0, 1))
blog.Info("after midnight")
clk.Set(day(22, 0, 2, 0))
blog.Info("later")
if n := warnings(buf); n != 2 {
t.Errorf("%d warnings over two failed dates, want 2:\n%s", n, buf.String())
}
})
}

View File

@@ -156,6 +156,12 @@ func Setup(cfg *compass.Config, opts Options) error {
func install(conf Config, ch *channels, opts Options) {
mu.Lock()
defer mu.Unlock()
installLocked(conf, ch, opts)
}
// installLocked makes ch the installed setup and retires the channels it
// replaces; the caller holds mu.
func installLocked(conf Config, ch *channels, opts Options) {
if saved == nil {
saved = &savedDefault{logger: slog.Default(), writer: log.Writer(), flags: log.Flags()}
}
@@ -174,7 +180,7 @@ func install(conf Config, ch *channels, opts Options) {
old := current.Swap(st)
slog.SetDefault(slog.New(newComponentHandler("")))
if old != nil {
closeChannels(old.channels)
retireChannels(old.channels)
}
}
@@ -186,6 +192,11 @@ func install(conf Config, ch *channels, opts Options) {
func SetVerbosity(v int, quiet bool) {
mu.Lock()
defer mu.Unlock()
setVerbosityLocked(v, quiet)
}
// setVerbosityLocked is SetVerbosity for a caller that holds mu.
func setVerbosityLocked(v int, quiet bool) {
verbosity.Store(int64(v))
st := current.Load()
if st == nil {
@@ -212,18 +223,21 @@ func FromGlobals(g bonfire.Globals) Options {
// the verbosity and quiet switch (SetVerbosity) and, when it differs from
// the one the channels were built with, the console color mode, which
// rebuilds the channels from the same configuration. The generated main
// registers it with bonfire.OnGlobals.
// registers it with bonfire.OnGlobals. It holds the setup lock for its
// whole run, so concurrent calls rebuild the channels once.
func ApplyGlobals(g bonfire.Globals) {
mu.Lock()
defer mu.Unlock()
st := current.Load()
if st != nil && st.opts.Color != g.Color {
opts := st.opts
opts.Color, opts.Verbosity, opts.Quiet = g.Color, g.Verbosity, g.Quiet
if ch, err := buildChannels(st.conf, opts); err == nil {
install(st.conf, ch, opts)
installLocked(st.conf, ch, opts)
return
}
}
SetVerbosity(g.Verbosity, g.Quiet)
setVerbosityLocked(g.Verbosity, g.Quiet)
}
type hideOnTTYKey struct{}
@@ -264,6 +278,22 @@ func Close() error {
return closeChannels(st.channels)
}
// retireChannels closes the files of channels a new setup replaced, for
// good: a goroutine that still holds the old state appends its record and
// the file is closed again, so no descriptor stays open.
func retireChannels(ch *channels) {
if ch == nil {
return
}
for _, c := range ch.closers {
if r, ok := c.(interface{ retire() error }); ok {
_ = r.retire()
continue
}
_ = c.Close()
}
}
func closeChannels(ch *channels) error {
if ch == nil {
return nil
@@ -275,9 +305,9 @@ func closeChannels(ch *channels) error {
return errors.Join(errs...)
}
// Reset drops the installed setup, closes its files and restores the process
// default logger (and the standard log package's output) captured at the
// first Bootstrap or Setup. Tests call it in t.Cleanup.
// Reset drops the installed setup, closes its files for good and restores
// the process default logger (and the standard log package's output)
// captured at the first Bootstrap or Setup. Tests call it in t.Cleanup.
func Reset() {
mu.Lock()
defer mu.Unlock()
@@ -290,6 +320,6 @@ func Reset() {
saved = nil
}
if old != nil {
closeChannels(old.channels)
retireChannels(old.channels)
}
}

535
modules/diary/diary_test.go Normal file
View File

@@ -0,0 +1,535 @@
package diary_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/diary"
)
// lockedBuffer is a bytes.Buffer safe for concurrent writers and readers.
type lockedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *lockedBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *lockedBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
func (b *lockedBuffer) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
b.buf.Reset()
}
// clock is an adjustable time source for Options.Now.
type clock struct {
mu sync.Mutex
t time.Time
}
func newClock(t time.Time) *clock { return &clock{t: t} }
func (c *clock) Now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.t
}
func (c *clock) Set(t time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.t = t
}
// day is a local time on 2026-10-<d> at hh:mm:ss.
func day(d, hh, mm, ss int) time.Time {
return time.Date(2026, 10, d, hh, mm, ss, 0, time.Local)
}
// setup runs diary.Setup with a configuration opened from environ (no
// config files) and registers diary.Reset as cleanup. Unset Options fields
// get test defaults: no color, a fresh root and a fixed clock.
func setup(t *testing.T, opts diary.Options, environ ...string) {
t.Helper()
if opts.Root == "" {
opts.Root = t.TempDir()
}
if opts.Environ == nil {
opts.Environ = []string{}
}
if opts.Now == nil {
opts.Now = func() time.Time { return day(21, 12, 0, 0) }
}
t.Cleanup(diary.Reset)
if err := diary.Setup(openConfig(t, environ...), opts); err != nil {
t.Fatalf("Setup: %v", err)
}
}
// keepDefault restores the process default logger and the log package's
// output and flags after the test.
func keepDefault(t *testing.T) {
t.Helper()
prev, w, flags := slog.Default(), log.Writer(), log.Flags()
t.Cleanup(func() {
slog.SetDefault(prev)
log.SetOutput(w)
log.SetFlags(flags)
})
}
// readDaily returns the lines of a daily file, closing the writers first.
func readDaily(t *testing.T, path string) []string {
t.Helper()
if err := diary.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read daily file: %v", err)
}
text := strings.TrimSpace(string(raw))
if text == "" {
return nil
}
return strings.Split(text, "\n")
}
// TestComponentFloors: a component's log.components level beats log.level,
// and records without a component use log.level.
func TestComponentFloors(t *testing.T) {
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf},
"SUMMER_LOG__DEFAULT=console",
"SUMMER_LOG__LEVEL=warn",
"SUMMER_LOG__COMPONENTS__JOURNAL=debug",
"SUMMER_LOG__COMPONENTS__RIVER=error",
)
diary.For("journal").Debug("journal debug")
diary.For("river").Warn("river warn")
diary.For("river").Error("river error")
diary.For("blog").Info("blog info")
diary.For("blog").Warn("blog warn")
slog.Info("default info")
slog.Warn("default warn")
out := buf.String()
for _, want := range []string{"journal debug", "river error", "blog warn", "default warn"} {
if !strings.Contains(out, want) {
t.Errorf("console lacks %q:\n%s", want, out)
}
}
for _, unwanted := range []string{"river warn", "blog info", "default info"} {
if strings.Contains(out, unwanted) {
t.Errorf("console has %q below its floor:\n%s", unwanted, out)
}
}
}
// TestVerbosityLevels: verbosity 1 lets info through for a warn component,
// 2 lets debug through, 3 adds source locations on styled console lines.
func TestVerbosityLevels(t *testing.T) {
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf}, "SUMMER_LOG__DEFAULT=console")
river, blog := diary.For("river"), diary.For("blog")
river.Info("river info v0")
river.Warn("river warn v0")
blog.Debug("blog debug v0")
diary.SetVerbosity(1, false)
if diary.Verbosity() != 1 {
t.Fatalf("Verbosity() = %d, want 1", diary.Verbosity())
}
river.Info("river info v1")
blog.Debug("blog debug v1")
diary.SetVerbosity(2, false)
river.Debug("river debug v2")
blog.Debug("blog debug v2")
out := buf.String()
for _, want := range []string{"river warn v0", "river info v1", "river debug v2", "blog debug v2"} {
if !strings.Contains(out, want) {
t.Errorf("console lacks %q:\n%s", want, out)
}
}
for _, unwanted := range []string{"river info v0", "blog debug v0", "blog debug v1"} {
if strings.Contains(out, unwanted) {
t.Errorf("console has %q:\n%s", unwanted, out)
}
}
// Source locations need the styled console.
var styled lockedBuffer
diary.Reset()
setup(t, diary.Options{Stderr: &styled, Color: bonfire.ColorAlways, Verbosity: 2})
blog.Info("no source at v2")
diary.SetVerbosity(3, false)
blog.Info("source at v3")
lines := strings.Split(strings.TrimSpace(styled.String()), "\n")
if len(lines) != 2 {
t.Fatalf("styled console has %d lines, want 2:\n%s", len(lines), styled.String())
}
if strings.Contains(lines[0], "diary_test.go:") {
t.Errorf("verbosity 2 line carries a source location: %q", lines[0])
}
if !strings.Contains(lines[1], "diary_test.go:") {
t.Errorf("verbosity 3 line lacks the source location: %q", lines[1])
}
// Options.Verbosity 3 at setup switches source on too.
styled.Reset()
diary.Reset()
setup(t, diary.Options{Stderr: &styled, Color: bonfire.ColorAlways, Verbosity: 3})
blog.Info("source from options")
if !strings.Contains(styled.String(), "diary_test.go:") {
t.Errorf("Options.Verbosity 3 line lacks the source location:\n%s", styled.String())
}
}
// TestQuietConsoleOnly: quiet raises the console channels to warn while the
// daily file keeps its own level.
func TestQuietConsoleOnly(t *testing.T) {
var buf lockedBuffer
root := t.TempDir()
setup(t, diary.Options{Stderr: &buf, Root: root, Quiet: true},
"SUMMER_LOG__CHANNELS__DAILY__LEVEL=info")
blog := diary.For("blog")
blog.Info("info while quiet")
blog.Warn("warn while quiet")
out := buf.String()
if strings.Contains(out, "info while quiet") {
t.Errorf("quiet console shows an info record:\n%s", out)
}
if !strings.Contains(out, "warn while quiet") {
t.Errorf("quiet console lacks the warning:\n%s", out)
}
lines := readDaily(t, filepath.Join(root, "storage", "logs", "summer-2026-10-21.log"))
daily := strings.Join(lines, "\n")
for _, want := range []string{"info while quiet", "warn while quiet"} {
if !strings.Contains(daily, want) {
t.Errorf("daily file lacks %q under quiet:\n%s", want, daily)
}
}
// Quiet beats verbosity on the console; leaving quiet restores info.
buf.Reset()
diary.SetVerbosity(2, true)
blog.Info("info at -vv -q")
diary.SetVerbosity(0, false)
blog.Info("info after quiet")
out = buf.String()
if strings.Contains(out, "info at -vv -q") {
t.Errorf("quiet console shows info at verbosity 2:\n%s", out)
}
if !strings.Contains(out, "info after quiet") {
t.Errorf("console lacks info after quiet was switched off:\n%s", out)
}
}
// preSetupLogger is created before any setup, as module loggers are.
var preSetupLogger = diary.For("early")
// TestForBeforeSetup: a logger created before setup writes to the process
// default logger until Setup, then to the configured channels, including
// the loggers derived from it with With and WithGroup.
func TestForBeforeSetup(t *testing.T) {
keepDefault(t)
diary.Reset()
var before bytes.Buffer
slog.SetDefault(slog.New(slog.NewTextHandler(&before, nil)))
derived := preSetupLogger.With("k", "v").WithGroup("g")
preSetupLogger.Info("before setup")
derived.Info("derived before", "a", 1)
for _, want := range []string{"component=early", "before setup", "k=v", "g.a=1"} {
if !strings.Contains(before.String(), want) {
t.Errorf("process default lacks %q:\n%s", want, before.String())
}
}
var after lockedBuffer
setup(t, diary.Options{Stderr: &after}, "SUMMER_LOG__DEFAULT=console")
before.Reset()
preSetupLogger.Info("after setup")
derived.Info("derived after", "a", 2)
if before.Len() != 0 {
t.Errorf("process default still receives records after Setup:\n%s", before.String())
}
for _, want := range []string{"component=early", "after setup", "derived after", "k=v", "g.a=2"} {
if !strings.Contains(after.String(), want) {
t.Errorf("configured console lacks %q:\n%s", want, after.String())
}
}
}
// TestResetRestoresDefault: Reset puts back the process default logger and
// the log package's output captured at the first setup.
func TestResetRestoresDefault(t *testing.T) {
keepDefault(t)
diary.Reset()
var own, std bytes.Buffer
custom := slog.New(slog.NewTextHandler(&own, nil))
slog.SetDefault(custom)
log.SetOutput(&std)
log.SetFlags(0)
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf, Verbosity: 2}, "SUMMER_LOG__DEFAULT=console")
if slog.Default() == custom {
t.Fatal("Setup did not install diary as the process default")
}
log.Print("through the log package")
if !strings.Contains(buf.String(), "through the log package") {
t.Errorf("log package output does not reach diary:\n%s", buf.String())
}
diary.Reset()
if slog.Default() != custom {
t.Error("Reset did not restore the previous process default logger")
}
if log.Writer() != &std {
t.Error("Reset did not restore the log package's output")
}
if diary.Verbosity() != 0 {
t.Errorf("Verbosity() = %d after Reset, want 0", diary.Verbosity())
}
diary.For("x").Info("after reset")
if !strings.Contains(own.String(), "after reset") {
t.Errorf("record after Reset did not reach the restored default:\n%s", own.String())
}
}
// TestNoRecursionWithoutState: with no setup and a diary logger as the
// process default, a component logger falls back instead of recursing.
func TestNoRecursionWithoutState(t *testing.T) {
keepDefault(t)
diary.Reset()
slog.SetDefault(diary.For("loop"))
done := make(chan struct{})
go func() {
defer close(done)
diary.For("x").Debug("dropped by the fallback level")
diary.For("x").Warn("diary fallback probe (expected on stderr)")
}()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("logging without state did not return")
}
}
// TestHideOnTTY: records logged with HideOnTTY are skipped by the styled
// console only; a plain console and the daily file keep them.
func TestHideOnTTY(t *testing.T) {
ctx := diary.HideOnTTY(context.Background())
if diary.HideOnTTY(nil) == nil { //nolint:staticcheck // a nil context is accepted
t.Fatal("HideOnTTY(nil) returned nil")
}
var styled lockedBuffer
root := t.TempDir()
setup(t, diary.Options{Stderr: &styled, Color: bonfire.ColorAlways, Root: root},
"SUMMER_LOG__CHANNELS__DAILY__LEVEL=info")
blog := diary.For("blog")
blog.InfoContext(ctx, "hidden summary")
blog.Info("shown line")
if strings.Contains(styled.String(), "hidden summary") {
t.Errorf("styled console shows a HideOnTTY record:\n%s", styled.String())
}
if !strings.Contains(styled.String(), "shown line") {
t.Errorf("styled console lacks the ordinary record:\n%s", styled.String())
}
daily := strings.Join(readDaily(t, filepath.Join(root, "storage", "logs", "summer-2026-10-21.log")), "\n")
if !strings.Contains(daily, "hidden summary") {
t.Errorf("daily file lacks the HideOnTTY record:\n%s", daily)
}
diary.Reset()
var plain lockedBuffer
setup(t, diary.Options{Stderr: &plain}, "SUMMER_LOG__DEFAULT=console")
blog.InfoContext(ctx, "hidden summary")
if !strings.Contains(plain.String(), "hidden summary") {
t.Errorf("plain console off a terminal dropped a HideOnTTY record:\n%s", plain.String())
}
}
// TestConcurrentWrites: 100 goroutines logging at once (with verbosity
// changes in between) leave every console line and every daily line intact.
func TestConcurrentWrites(t *testing.T) {
const writers, perWriter = 100, 20
var buf lockedBuffer
root := t.TempDir()
setup(t, diary.Options{Stderr: &buf, Root: root}, "SUMMER_LOG__CHANNELS__DAILY__LEVEL=info")
logger := diary.For("worker")
var wg sync.WaitGroup
for g := range writers {
wg.Add(1)
go func() {
defer wg.Done()
l := logger.With("g", g)
for n := range perWriter {
l.Info("concurrent write", "n", n)
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
for i := range 50 {
diary.SetVerbosity(i%2, false)
}
}()
wg.Wait()
line := regexp.MustCompile(`^time=\S+ level=INFO msg="concurrent write" component=worker g=(\d+) n=(\d+)$`)
seen := map[string]bool{}
for _, l := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
m := line.FindStringSubmatch(l)
if m == nil {
t.Fatalf("garbled console line: %q", l)
}
seen[m[1]+"/"+m[2]] = true
}
if len(seen) != writers*perWriter {
t.Errorf("console has %d distinct records, want %d", len(seen), writers*perWriter)
}
lines := readDaily(t, filepath.Join(root, "storage", "logs", "summer-2026-10-21.log"))
if len(lines) != writers*perWriter {
t.Fatalf("daily file has %d lines, want %d", len(lines), writers*perWriter)
}
daily := map[string]bool{}
for _, l := range lines {
var rec map[string]any
if err := json.Unmarshal([]byte(l), &rec); err != nil {
t.Fatalf("garbled daily line %q: %v", l, err)
}
if rec["msg"] != "concurrent write" || rec["component"] != "worker" {
t.Fatalf("unexpected daily record %v", rec)
}
daily[fmt.Sprint(rec["g"], "/", rec["n"])] = true
}
if len(daily) != writers*perWriter {
t.Errorf("daily file has %d distinct records, want %d", len(daily), writers*perWriter)
}
}
// TestRedactionEveryDriver: every writing driver redacts sensitive
// attributes and scrubs Bearer tokens from the message; null writes nothing.
func TestRedactionEveryDriver(t *testing.T) {
secrets := []string{"pw-value-1", "tok-value-2", "key-value-3", "pw-nested-4", "abc.def.ghi"}
logSecrets := func() {
diary.For("auth").Warn("calling upstream with Bearer abc.def.ghi",
"password", "pw-value-1", "token", "tok-value-2", "api_key", "key-value-3",
slog.Group("nested", "password", "pw-nested-4"))
}
check := func(t *testing.T, where, out string) {
t.Helper()
for _, s := range secrets {
if strings.Contains(out, s) {
t.Errorf("%s leaks %q:\n%s", where, s, out)
}
}
if strings.Count(out, "[REDACTED]") < 5 {
t.Errorf("%s has fewer than five [REDACTED] markers:\n%s", where, out)
}
if !strings.Contains(out, "Bearer [REDACTED]") {
t.Errorf("%s does not scrub the Bearer token:\n%s", where, out)
}
}
dailyFile := func(root string) string {
return filepath.Join(root, "storage", "logs", "summer-2026-10-21.log")
}
cases := []struct {
name string
color bonfire.ColorMode
environ []string
console bool
daily bool
}{
{"console logfmt", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=console"}, true, false},
{"console styled", bonfire.ColorAlways, []string{"SUMMER_LOG__DEFAULT=console"}, true, false},
{"console json", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=console", "SUMMER_LOG__CHANNELS__CONSOLE__FORMAT=json"}, true, false},
{"stderr logfmt", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=stderr"}, true, false},
{"stderr json", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=stderr", "SUMMER_LOG__CHANNELS__STDERR__FORMAT=json"}, true, false},
{"daily", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=daily"}, false, true},
{"daily logfmt", bonfire.ColorNever, []string{"SUMMER_LOG__DEFAULT=daily", "SUMMER_LOG__CHANNELS__DAILY__FORMAT=logfmt"}, false, true},
{"stack", bonfire.ColorNever, nil, true, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var buf lockedBuffer
root := t.TempDir()
setup(t, diary.Options{Stderr: &buf, Root: root, Color: tc.color}, tc.environ...)
logSecrets()
if tc.console {
check(t, "console", buf.String())
} else if buf.String() != "" {
t.Errorf("console written although the channel is %s:\n%s", tc.name, buf.String())
}
if tc.daily {
check(t, "daily file", strings.Join(readDaily(t, dailyFile(root)), "\n"))
}
})
}
t.Run("null", func(t *testing.T) {
var buf lockedBuffer
root := t.TempDir()
setup(t, diary.Options{Stderr: &buf, Root: root}, "SUMMER_LOG__DEFAULT=null")
logSecrets()
if buf.String() != "" {
t.Errorf("null channel wrote to the console:\n%s", buf.String())
}
entries, err := os.ReadDir(root)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Errorf("null channel created files: %v", entries)
}
})
}
// openYAMLConfig opens a compass config whose config/log.yaml holds text.
func openYAMLConfig(t *testing.T, text string, environ ...string) *compass.Config {
t.Helper()
dir := filepath.Join(t.TempDir(), "config")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "log.yaml"), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := compass.Open(compass.Options{Dir: dir, Environ: append([]string{}, environ...)})
if err != nil {
t.Fatal(err)
}
return cfg
}

View File

@@ -0,0 +1,123 @@
package diary
import (
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
// installDefault builds the default configuration (console and daily) on
// root and installs it, as Setup does.
func installDefault(t *testing.T, root string, color bonfire.ColorMode) *channels {
t.Helper()
opts := Options{
Root: root,
Stderr: io.Discard,
Environ: []string{},
Color: color,
Now: func() time.Time { return time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) },
}.normalize()
conf := DefaultConfig()
ch, err := buildChannels(conf, opts)
if err != nil {
t.Fatal(err)
}
install(conf, ch, opts)
return ch
}
// dailyOf returns the daily writer of built channels.
func dailyOf(t *testing.T, ch *channels) *dailyWriter {
t.Helper()
for _, c := range ch.closers {
if d, ok := c.(*dailyWriter); ok {
return d
}
}
t.Fatal("no daily writer in the channels")
return nil
}
func (d *dailyWriter) hasFile() bool {
d.mu.Lock()
defer d.mu.Unlock()
return d.file != nil
}
// TestRetiredDailyWriterKeepsNoFile (WR-06): a daily writer replaced by a
// new setup is retired: a late record through it (from a goroutine that
// still holds the old state) is appended to the day's file and the file is
// closed again. diary.Close keeps letting the installed writers reopen.
func TestRetiredDailyWriterKeepsNoFile(t *testing.T) {
t.Cleanup(Reset)
root := t.TempDir()
file := filepath.Join(root, "storage", "logs", "summer-2026-10-21.log")
old := dailyOf(t, installDefault(t, root, bonfire.ColorNever))
if _, err := old.Write([]byte("first\n")); err != nil {
t.Fatal(err)
}
installed := dailyOf(t, installDefault(t, root, bonfire.ColorNever))
if old.hasFile() {
t.Fatal("the replaced writer still holds its file")
}
if _, err := old.Write([]byte("late\n")); err != nil {
t.Fatal(err)
}
if old.hasFile() {
t.Error("a late record through the replaced writer left its file open")
}
if _, err := installed.Write([]byte("current\n")); err != nil {
t.Fatal(err)
}
if err := Close(); err != nil {
t.Fatal(err)
}
if _, err := installed.Write([]byte("reopened\n")); err != nil {
t.Fatal(err)
}
if !installed.hasFile() {
t.Error("the installed writer did not reopen its file after diary.Close")
}
raw, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
if got := string(raw); got != "first\nlate\ncurrent\nreopened\n" {
t.Errorf("daily file = %q", got)
}
}
// TestApplyGlobalsRebuildsOnce (WR-06): concurrent ApplyGlobals calls with
// a new color mode rebuild the channels once, and every call's verbosity
// is applied.
func TestApplyGlobalsRebuildsOnce(t *testing.T) {
t.Cleanup(Reset)
installDefault(t, t.TempDir(), bonfire.ColorAuto)
before := gens.Load()
var start, done sync.WaitGroup
start.Add(1)
for range 50 {
done.Add(1)
go func() {
defer done.Done()
start.Wait()
ApplyGlobals(bonfire.Globals{Color: bonfire.ColorNever, Verbosity: 2})
}()
}
start.Done()
done.Wait()
if n := gens.Load() - before; n != 1 {
t.Errorf("the channels were rebuilt %d times, want 1", n)
}
st := current.Load()
if st.opts.Color != bonfire.ColorNever || st.verbosity != 2 || Verbosity() != 2 {
t.Errorf("state after ApplyGlobals: color %v, verbosity %d (Verbosity %d)", st.opts.Color, st.verbosity, Verbosity())
}
}

View File

@@ -0,0 +1,159 @@
package diary_test
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"regexp"
"strconv"
"strings"
"testing"
"testing/slogtest"
"git.golem15.com/golem15/summercms/modules/diary"
"github.com/charmbracelet/colorprofile"
)
// TestSlogtestConsoleHandler: the styled console handler (with the NoTTY
// profile, so its lines carry no escape codes) passes testing/slogtest.
func TestSlogtestConsoleHandler(t *testing.T) {
var buf bytes.Buffer
h := diary.NewConsoleHandler(&buf, diary.ConsoleOptions{Profile: colorprofile.NoTTY})
results := func() []map[string]any {
var out []map[string]any
for _, line := range strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n") {
if line == "" {
continue
}
m, err := parseConsoleLine(line)
if err != nil {
t.Fatalf("unparsable console line %q: %v", line, err)
}
out = append(out, m)
}
return out
}
if err := slogtest.TestHandler(h, results); err != nil {
t.Fatal(err)
}
}
// TestSlogtestPipeline: the full pipeline (component handler, sunscreen and
// a stack of a JSON stderr channel and a null channel) passes
// testing/slogtest through its JSON output, for a component logger and for
// the process default.
func TestSlogtestPipeline(t *testing.T) {
for _, name := range []string{"pipeline", ""} {
t.Run(fmt.Sprintf("component %q", name), func(t *testing.T) {
var buf lockedBuffer
setup(t, diary.Options{Stderr: &buf},
"SUMMER_LOG__DEFAULT=stack",
"SUMMER_LOG__CHANNELS__STACK__CHANNELS=stderr,null",
"SUMMER_LOG__CHANNELS__STDERR__FORMAT=json",
)
h := diary.For(name).Handler()
if name == "" {
h = slog.Default().Handler()
}
results := func() []map[string]any {
var out []map[string]any
for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
var m map[string]any
if err := json.Unmarshal([]byte(line), &m); err != nil {
t.Fatalf("pipeline line is not JSON: %q: %v", line, err)
}
if name != "" && m[diary.ComponentKey] != name {
t.Fatalf("pipeline record lacks component %q: %v", name, m)
}
out = append(out, m)
}
return out
}
if err := slogtest.TestHandler(h, results); err != nil {
t.Fatal(err)
}
})
}
}
var consoleTime = regexp.MustCompile(`^\d{2}:\d{2}:\d{2} `)
// parseConsoleLine reads one console line without a component: an optional
// HH:MM:SS time, the level, the message (the words before the first
// key=value field) and key=value fields whose dotted keys become nested
// maps. Quoted values are unquoted.
func parseConsoleLine(line string) (map[string]any, error) {
m := map[string]any{}
if loc := consoleTime.FindStringIndex(line); loc != nil {
m[slog.TimeKey] = strings.TrimSpace(line[:loc[1]])
line = line[loc[1]:]
}
level, rest, ok := strings.Cut(line, " ")
if !ok {
return nil, fmt.Errorf("no level")
}
m[slog.LevelKey] = level
tokens, err := splitFields(strings.TrimLeft(rest, " "))
if err != nil {
return nil, err
}
var msg []string
i := 0
for ; i < len(tokens) && !strings.Contains(tokens[i], "="); i++ {
msg = append(msg, tokens[i])
}
m[slog.MessageKey] = strings.Join(msg, " ")
for ; i < len(tokens); i++ {
key, raw, _ := strings.Cut(tokens[i], "=")
value := raw
if strings.HasPrefix(raw, `"`) {
if value, err = strconv.Unquote(raw); err != nil {
return nil, fmt.Errorf("value of %s: %w", key, err)
}
}
parts := strings.Split(key, ".")
cur := m
for _, p := range parts[:len(parts)-1] {
next, ok := cur[p].(map[string]any)
if !ok {
next = map[string]any{}
cur[p] = next
}
cur = next
}
cur[parts[len(parts)-1]] = value
}
return m, nil
}
// splitFields splits s on spaces, keeping a double-quoted value (with
// backslash escapes) inside its field.
func splitFields(s string) ([]string, error) {
var out []string
for s != "" {
var b strings.Builder
for s != "" && s[0] != ' ' {
if s[0] == '"' {
end := 1
for end < len(s) && s[end] != '"' {
if s[end] == '\\' {
end++
}
end++
}
if end >= len(s) {
return nil, fmt.Errorf("unterminated quote")
}
b.WriteString(s[:end+1])
s = s[end+1:]
continue
}
b.WriteByte(s[0])
s = s[1:]
}
out = append(out, b.String())
s = strings.TrimLeft(s, " ")
}
return out, nil
}

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
@@ -366,3 +367,58 @@ func TestTestPushCommand(t *testing.T) {
noSecret(t, out)
})
}
// TestFlareReported: a failing generate or test-push prints its own error
// line once and returns an error matching bonfire.ErrReported, so the
// binary exits 1 without a second error block.
func TestFlareReported(t *testing.T) {
keys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
withKeys, _ := newCommandApp(t, map[string]string{"push.yaml": pushYAML(true, keys)})
withSource, _ := newCommandApp(t, map[string]string{"push.yaml": pushYAML(true, keys)})
publishSource(t, withSource, &fakeSource{})
cases := []struct {
name string
app *backpack.App
args []string
line string
}{
{"generate --update without a config", backpack.New(nil), []string{GenerateVAPIDKeysCommandName, "--update"}, "Failed to save the keys: no configuration loaded"},
{"test-push with a bad user id", withKeys, []string{TestPushCommandName, "abc"}, "User abc not found"},
{"test-push without a source", withKeys, []string{TestPushCommandName, "5"}, "no subscription source registered"},
{"test-push for an unknown user", withSource, []string{TestPushCommandName, "9"}, "User 9 not found"},
{"test-push without subscriptions", withSource, []string{TestPushCommandName, "5"}, "No push subscriptions found for this user"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var out, errW bytes.Buffer
root, err := bonfire.NewRootIO("acme", Commands(tc.app), strings.NewReader(""), &out, &errW)
if err != nil {
t.Fatal(err)
}
if code := bonfire.Execute(context.Background(), root, tc.args); code != bonfire.ExitFailure {
t.Fatalf("exit %d\n%s%s", code, out.String(), errW.String())
}
if n := strings.Count(errW.String(), tc.line); n != 1 {
t.Errorf("error line %q printed %d times:\n%s", tc.line, n, errW.String())
}
if strings.Contains(errW.String(), "ERROR") || strings.Contains(out.String(), "ERROR") {
t.Errorf("a reported failure printed an error block:\n%s%s", out.String(), errW.String())
}
var direct bytes.Buffer
var cmd bonfire.Command
for _, c := range Commands(tc.app) {
if c.Name == tc.args[0] {
cmd = c
}
}
err = bonfire.Call(context.Background(), []bonfire.Command{cmd}, cmd.Name, tc.args[1:], &direct)
if !errors.Is(err, bonfire.ErrReported) {
t.Errorf("error %v does not match bonfire.ErrReported", err)
}
})
}
}

View File

@@ -256,7 +256,7 @@ storage:
| Command | Flags | Description |
|---------|-------|-------------|
| `migrate` | `--pretend`, `--force` | Runs the framework migrations (`system_files`, the backend admin tables, `deferred_bindings` and the job queue), then each plugin's migrations in dependency order; prints `<set>: N applied` per set and `Applied N migrations`, or `Nothing to migrate`. `--pretend` prints each pending migration as `<plugin>: <id>` and `N migrations would run`, and applies nothing. In production on an interactive terminal it asks first (`--force` skips the question); off a terminal it never asks. |
| `migrate` | `--pretend`, `--force` | Runs the framework migrations (`system_files`, the backend admin tables, `deferred_bindings` and the job queue), then each plugin's migrations in dependency order; prints `<set>: N applied` per set and `Applied N migrations`, or `Nothing to migrate`. The applied lines count what was actually applied: when a migration fails, `migrate` prints the lines for the sets applied before it, then the error, and no `Applied` line. `--pretend` prints each pending migration as `<plugin>: <id>` and `N migrations would run`, and applies nothing. In production on an interactive terminal it asks first (`--force` skips the question); off a terminal it never asks. |
| `migrate:rollback` | `--plugin <id>`, `--pretend`, `--force` | Rolls back the last applied migration of the given plugin; without the flag, of the last activated plugin that has migrations. Prints `Rolled back <id> of <plugin>`; `--pretend` names the migration and changes nothing. Production confirmation as for `migrate`. |
| `migrate:status` | `--json` | Prints a table of plugin, history table and applied migration IDs. `--json` prints a JSON array of every declared migration of those plugins: `plugin`, `table`, `id`, `applied`. |
| `deferred:purge` | `--days <n>`, `--pretend`, `--force` | Deletes deferred bindings older than `n` days (default `database.deferred_bindings.purge_days`, else 5), with the unattached uploads they hold (blobs after the commit) and the records created under deferral, and prints the counts. Records that were only linked are kept. Model types come from every plugin's `pact.HasModels`; the uploads bucket from the published one or `storage.uploads.bucket_url`. `--pretend` counts the bindings past the cut-off and deletes nothing; production confirmation as for `migrate`. |

View File

@@ -103,6 +103,22 @@ func lastApplied(gdb *gorm.DB, plugins []party.Plugin, pluginID string) (string,
return "", nil
}
// appliedSince returns the migrations of before that after no longer
// lists as pending, in the order of before.
func appliedSince(before, after []MigrationRef) []MigrationRef {
still := make(map[MigrationRef]bool, len(after))
for _, ref := range after {
still[ref] = true
}
var applied []MigrationRef
for _, ref := range before {
if !still[ref] {
applied = append(applied, ref)
}
}
return applied
}
// statusJSON is one migration of migrate:status --json.
type statusJSON struct {
Plugin string `json:"plugin"`
@@ -154,12 +170,18 @@ asks.`,
if err := confirmProduction(app, in, out, fmt.Sprintf("Apply %d migrations to the production database?", len(pending)), "Migration cancelled."); err != nil {
return err
}
if err := Migrate(gdb, plugins); err != nil {
return err
migrateErr := Migrate(gdb, plugins)
// Report what Migrate actually applied: the pending
// migrations before it ran that are no longer pending,
// also when it failed partway.
after, err := PendingMigrations(gdb, plugins)
if err != nil {
return errors.Join(migrateErr, err)
}
applied := appliedSince(pending, after)
counts := map[string]int{}
var order []string
for _, ref := range pending {
for _, ref := range applied {
if counts[ref.Plugin] == 0 {
order = append(order, ref.Plugin)
}
@@ -168,7 +190,10 @@ asks.`,
for _, plugin := range order {
out.Info(fmt.Sprintf("%s: %d applied", plugin, counts[plugin]))
}
out.Success(fmt.Sprintf("Applied %d migrations", len(pending)))
if migrateErr != nil {
return migrateErr
}
out.Success(fmt.Sprintf("Applied %d migrations", len(applied)))
return nil
})
},

View File

@@ -0,0 +1,133 @@
package lagoon
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/compass"
)
// envApp is an app in environment env with a fixed test app key and an
// optional DSN.
func envApp(t *testing.T, env, dsn string, extra ...string) *backpack.App {
t.Helper()
environ := append([]string{
"SUMMER_ENV=" + env,
"SUMMER_APP__KEY=" + base64.StdEncoding.EncodeToString(bytes.Repeat([]byte("k"), 32)),
}, extra...)
if dsn != "" {
environ = append(environ, "SUMMER_DATABASE__DSN="+dsn)
}
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Env: env, Environ: environ})
if err != nil {
t.Fatal(err)
}
return backpack.New(cfg)
}
// probeConfirm is a command that runs confirmProduction, as migrate does.
func probeConfirm(app *backpack.App) bonfire.Command {
return bonfire.Command{
Name: "probe:confirm",
Description: "Ask like migrate does",
Flags: []bonfire.Flag{{Name: "force", Description: forceDescription, Bare: true}},
Run: func(_ context.Context, in bonfire.Input, out bonfire.Output) error {
if err := confirmProduction(app, in, out, "Apply 3 migrations to the production database?", "Migration cancelled."); err != nil {
return err
}
out.Println("proceeded")
return nil
},
}
}
// TestConfirmProductionNonTTYProceeds: in production a console that is not
// an interactive terminal never asks and never reads stdin, so migrate,
// migrate:rollback and deferred:purge proceed in deploys and cron;
// bonfire.Call does the same. Every destructive command offers --force.
func TestConfirmProductionNonTTYProceeds(t *testing.T) {
app := envApp(t, "production", "")
in := strings.NewReader("n\n")
var out, errW bytes.Buffer
root, err := bonfire.NewRootIO("acme", []bonfire.Command{probeConfirm(app)}, in, &out, &errW)
if err != nil {
t.Fatal(err)
}
if code := bonfire.Execute(context.Background(), root, []string{"probe:confirm"}); code != bonfire.ExitOK {
t.Fatalf("exit %d\n%s", code, errW.String())
}
if in.Len() != 2 || out.String() != "proceeded\n" {
t.Fatalf("stdin left %d, output %q: the probe asked or did not proceed", in.Len(), out.String())
}
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), []bonfire.Command{probeConfirm(app)}, "probe:confirm", nil, &buf); err != nil || buf.String() != "proceeded\n" {
t.Fatalf("Call: %v, %q", err, buf.String())
}
for _, c := range RuntimeCommands(app, nil) {
switch c.Name {
case "migrate", "migrate:rollback", "deferred:purge":
force := false
for _, f := range c.Flags {
if f.Name == "force" && f.Bare {
force = true
}
}
if !force || !strings.Contains(c.Long, "--force skips the") || !strings.Contains(c.Long, "production") {
t.Errorf("%s does not document the production question and --force", c.Name)
}
}
}
}
// TestKeyGenerateJSON: key:generate --json prints {"key": "..."} with a
// 32-byte base64 key, under -q too.
func TestKeyGenerateJSON(t *testing.T) {
seen := map[string]bool{}
for _, args := range [][]string{{"--json"}, {"--json", "-q"}} {
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), []bonfire.Command{KeyGenerateCommand()}, "key:generate", args, &buf); err != nil {
t.Fatal(err)
}
var got map[string]string
if err := json.Unmarshal(buf.Bytes(), &got); err != nil || len(got) != 1 {
t.Fatalf("%v: not {\"key\": ...}: %v\n%s", args, err, buf.String())
}
raw, err := base64.StdEncoding.DecodeString(got["key"])
if err != nil || len(raw) != 32 {
t.Fatalf("%v: key %q is not 32 base64 bytes", args, got["key"])
}
if seen[got["key"]] {
t.Fatal("key:generate printed the same key twice")
}
seen[got["key"]] = true
}
}
// TestKeyGenerateQuiet: under -q key:generate prints the bare key and a
// newline (KEY=$(bin/acme key:generate -q)); without it the key follows
// the success mark.
func TestKeyGenerateQuiet(t *testing.T) {
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), []bonfire.Command{KeyGenerateCommand()}, "key:generate", []string{"-q"}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
key := strings.TrimSuffix(out, "\n")
if strings.Count(out, "\n") != 1 || strings.ContainsAny(key, " ✓") || len(key) != 44 {
t.Fatalf("-q output = %q, want the bare 44-character key", out)
}
buf.Reset()
if err := bonfire.Call(context.Background(), []bonfire.Command{KeyGenerateCommand()}, "key:generate", nil, &buf); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(buf.String(), "✓ ") || len(strings.TrimSpace(strings.TrimPrefix(buf.String(), "✓ "))) != 44 {
t.Fatalf("default output = %q", buf.String())
}
}

View File

@@ -0,0 +1,83 @@
package lagoon
import (
"bytes"
"context"
"errors"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/diary"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// TestGormLoggerLevels: GORM logs at warn by default and info (SQL traces)
// at verbosity 3; a record-not-found error is not logged; slow queries and
// other errors are; and traces carry parameterized SQL, never bound values.
func TestGormLoggerLevels(t *testing.T) {
var buf bytes.Buffer
t.Cleanup(diary.Reset)
diary.Bootstrap(&buf, diary.Options{Color: bonfire.ColorNever})
ctx := context.Background()
trace := func(l logger.Interface, begin time.Time, err error) {
l.Trace(ctx, begin, func() (string, int64) { return `SELECT * FROM "users" WHERE email = $1`, 1 }, err)
}
l := gormLogger()
trace(l, time.Now(), nil)
trace(l, time.Now(), gorm.ErrRecordNotFound)
if buf.Len() != 0 {
t.Fatalf("verbosity 0 logged a fast trace or record-not-found:\n%s", buf.String())
}
trace(l, time.Now(), errors.New("relation does not exist"))
trace(l, time.Now().Add(-time.Second), nil)
out := buf.String()
for _, want := range []string{"level=ERROR", "relation does not exist", "level=WARN", "component=db", `trace.sql="SELECT * FROM \"users\" WHERE email = $1"`} {
if !strings.Contains(out, want) {
t.Errorf("verbosity 0 output lacks %q:\n%s", want, out)
}
}
buf.Reset()
diary.SetVerbosity(3, false)
l = gormLogger()
trace(l, time.Now(), nil)
trace(l, time.Now(), gorm.ErrRecordNotFound)
out = buf.String()
if strings.Count(out, "SQL executed") != 2 || strings.Contains(out, "record not found") {
t.Errorf("verbosity 3: want two info traces and no record-not-found error:\n%s", out)
}
// The SQL in a trace keeps its placeholders: GORM asks the logger to
// filter the parameters, and lagoon's logger drops them.
filter, ok := l.(gorm.ParamsFilter)
if !ok {
t.Fatal("lagoon's GORM logger does not filter parameters")
}
if _, params := filter.ParamsFilter(ctx, "SELECT $1", "secret-value"); params != nil {
t.Errorf("ParamsFilter kept the bound values %v", params)
}
// End to end with a dry-run GORM session (no database is reached).
buf.Reset()
db, err := gorm.Open(postgres.New(postgres.Config{DSN: "host=127.0.0.1 port=1 user=nobody dbname=none sslmode=disable"}),
&gorm.Config{Logger: l, DryRun: true, DisableAutomaticPing: true})
if err != nil {
t.Fatalf("dry-run gorm: %v", err)
}
var row struct{ ID int }
db.Table("users").Where("email = ?", "ada@example.test").Where("password = ?", "secret-value").Find(&row)
out = buf.String()
if !strings.Contains(out, "SQL executed") || !strings.Contains(out, "$1") || !strings.Contains(out, "$2") {
t.Errorf("dry-run trace lacks the parameterized SQL:\n%s", out)
}
for _, leaked := range []string{"ada@example.test", "secret-value"} {
if strings.Contains(out, leaked) {
t.Errorf("dry-run trace leaks the bound value %q:\n%s", leaked, out)
}
}
}

View File

@@ -60,58 +60,23 @@ func migrator(gdb *gorm.DB, pluginID string, migrations []*gormigrate.Migration)
// Migrate runs the framework-owned system_files, backend-admin,
// deferred-binding (DeferredBindingMigrations, history DeferredHistoryID) and
// job-queue (QueueMigrations) sets first, then each plugin's HasMigrations set
// in party.Activate order.
// in party.Activate order. It applies the sets migrationSets lists, the list
// PendingMigrations reads, and stops at the first set that fails.
func Migrate(gdb *gorm.DB, plugins []party.Plugin) error {
if gdb == nil {
return fmt.Errorf("lagoon: gorm db is nil")
}
m, err := migrator(gdb, "summercms.attach", attach.Migrations)
sets, err := migrationSets(gdb, plugins)
if err != nil {
return err
}
if err := m.Migrate(); err != nil {
return fmt.Errorf("lagoon: migrate system_files: %w", err)
}
admin, err := migrator(gdb, "summercms.cabana", BackendAdminMigrations)
if err != nil {
return err
}
if err := admin.Migrate(); err != nil {
return fmt.Errorf("lagoon: migrate backend admin: %w", err)
}
deferred, err := migrator(gdb, DeferredHistoryID, DeferredBindingMigrations)
if err != nil {
return err
}
if err := deferred.Migrate(); err != nil {
return fmt.Errorf("lagoon: migrate deferred bindings: %w", err)
}
sqlDB, err := gdb.DB()
if err != nil {
return fmt.Errorf("lagoon: migrate queue: %w", err)
}
queue, err := migrator(gdb, QueueHistoryID, QueueMigrations(sqlDB))
if err != nil {
return err
}
if err := queue.Migrate(); err != nil {
return fmt.Errorf("lagoon: migrate queue: %w", err)
}
for _, p := range plugins {
hm, ok := p.(pact.HasMigrations)
if !ok {
continue
}
migrations := hm.Migrations()
if len(migrations) == 0 {
continue
}
m, err := migrator(gdb, p.ID(), migrations)
for _, s := range sets {
m, err := migrator(gdb, s.id, s.migrations)
if err != nil {
return err
}
if err := m.Migrate(); err != nil {
return fmt.Errorf("lagoon: migrate %s: %w", p.ID(), err)
return fmt.Errorf("lagoon: migrate %s: %w", s.label, err)
}
}
return nil
@@ -211,7 +176,10 @@ type MigrationRef struct {
// migrationSet is one history table's declared migrations.
type migrationSet struct {
id string
id string
// label names the set in Migrate's errors: "system_files", "backend
// admin", "deferred bindings", "queue" or the plugin ID.
label string
migrations []*gormigrate.Migration
}
@@ -224,10 +192,10 @@ func migrationSets(gdb *gorm.DB, plugins []party.Plugin) ([]migrationSet, error)
return nil, fmt.Errorf("lagoon: migration sets: %w", err)
}
sets := []migrationSet{
{"summercms.attach", attach.Migrations},
{"summercms.cabana", BackendAdminMigrations},
{DeferredHistoryID, DeferredBindingMigrations},
{QueueHistoryID, QueueMigrations(sqlDB)},
{"summercms.attach", "system_files", attach.Migrations},
{"summercms.cabana", "backend admin", BackendAdminMigrations},
{DeferredHistoryID, "deferred bindings", DeferredBindingMigrations},
{QueueHistoryID, "queue", QueueMigrations(sqlDB)},
}
for _, p := range plugins {
hm, ok := p.(pact.HasMigrations)
@@ -235,7 +203,7 @@ func migrationSets(gdb *gorm.DB, plugins []party.Plugin) ([]migrationSet, error)
continue
}
if ms := hm.Migrations(); len(ms) > 0 {
sets = append(sets, migrationSet{p.ID(), ms})
sets = append(sets, migrationSet{p.ID(), p.ID(), ms})
}
}
return sets, nil

View File

@@ -0,0 +1,352 @@
package lagoon
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/party"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// pendingPlugins are two plugins with one and two migrations.
func pendingPlugins() (alpha, beta migPlugin) {
table := func(name string) *gormigrate.Migration {
return &gormigrate.Migration{
ID: "202610210001_create_" + name,
Migrate: func(tx *gorm.DB) error { return tx.Exec(`CREATE TABLE ` + name + ` (id BIGSERIAL PRIMARY KEY)`).Error },
Rollback: func(tx *gorm.DB) error { return tx.Exec(`DROP TABLE IF EXISTS ` + name).Error },
}
}
seed := &gormigrate.Migration{
ID: "202610210002_seed_pending_beta",
Migrate: func(tx *gorm.DB) error { return tx.Exec(`INSERT INTO pending_beta DEFAULT VALUES`).Error },
Rollback: func(tx *gorm.DB) error { return tx.Exec(`DELETE FROM pending_beta`).Error },
}
return migPlugin{id: "demo.alpha", migrations: []*gormigrate.Migration{table("pending_alpha")}},
migPlugin{id: "demo.beta", migrations: []*gormigrate.Migration{table("pending_beta"), seed}}
}
// pluginRefs returns the refs of pending that belong to plugin IDs (the
// framework sets come first and are left out).
func pluginRefs(pending []MigrationRef) []string {
var out []string
for _, r := range pending {
if strings.HasPrefix(r.Plugin, "demo.") {
out = append(out, r.Plugin+":"+r.ID)
}
}
return out
}
// runLagoon runs one runtime command on a fresh app (a non-terminal stdin
// holding "n", which a question would consume) and returns stdout, stderr,
// the exit code and what is left of stdin.
func runLagoon(t *testing.T, env, dsn string, plugins []party.Plugin, args ...string) (string, string, int, int) {
t.Helper()
app := envApp(t, env, dsn, "SUMMER_STORAGE__UPLOADS__BUCKET_URL=file://"+t.TempDir())
in := strings.NewReader("n\n")
var out, errW bytes.Buffer
root, err := bonfire.NewRootIO("acme", RuntimeCommands(app, plugins), in, &out, &errW)
if err != nil {
t.Fatal(err)
}
code := bonfire.Execute(context.Background(), root, args)
return out.String(), errW.String(), code, in.Len()
}
// TestPendingMigrations: with no history every migration is pending,
// framework sets first, then plugins in order; applied IDs drop out, a
// partly applied set keeps only the rest, and a missing history table
// makes its whole set pending.
func TestPendingMigrations(t *testing.T) {
db, _ := dedicatedDB(t, "lagoon_pending")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
alpha, beta := pendingPlugins()
plugins := []party.Plugin{alpha, beta}
all, err := PendingMigrations(gdb, plugins)
if err != nil {
t.Fatal(err)
}
want := []string{"demo.alpha:202610210001_create_pending_alpha", "demo.beta:202610210001_create_pending_beta", "demo.beta:202610210002_seed_pending_beta"}
if got := pluginRefs(all); strings.Join(got, ",") != strings.Join(want, ",") {
t.Fatalf("plugin refs = %v, want %v", got, want)
}
if len(all) <= len(want) || strings.HasPrefix(all[0].Plugin, "demo.") {
t.Fatalf("framework sets do not come first: %v", all)
}
for _, ref := range all[:len(all)-len(want)] {
if strings.HasPrefix(ref.Plugin, "demo.") {
t.Fatalf("a plugin ref sits among the framework sets: %v", all)
}
}
// Framework and alpha applied: only beta is pending, its history table
// still missing.
if err := Migrate(gdb, []party.Plugin{alpha}); err != nil {
t.Fatal(err)
}
if gdb.Migrator().HasTable("summer_migrations_demo_beta") {
t.Fatal("beta's history table exists before beta migrated")
}
some, err := PendingMigrations(gdb, plugins)
if err != nil {
t.Fatal(err)
}
if strings.Join(pluginRefs(some), ",") != strings.Join(want[1:], ",") || len(some) != 2 {
t.Fatalf("after alpha: %v, want only beta's two", some)
}
// beta applied, then its seed removed from the history: one pending.
if err := Migrate(gdb, plugins); err != nil {
t.Fatal(err)
}
if err := gdb.Exec(`DELETE FROM summer_migrations_demo_beta WHERE id = ?`, "202610210002_seed_pending_beta").Error; err != nil {
t.Fatal(err)
}
partly, err := PendingMigrations(gdb, plugins)
if err != nil {
t.Fatal(err)
}
if len(partly) != 1 || partly[0] != (MigrationRef{Plugin: "demo.beta", ID: "202610210002_seed_pending_beta"}) {
t.Fatalf("partly applied = %v", partly)
}
if _, err := PendingMigrations(nil, plugins); err == nil {
t.Fatal("a nil db did not fail")
}
}
// TestMigratePretend: migrate --pretend lists every pending migration and
// applies nothing; migrate in production off a terminal then applies them
// without asking (stdin unread), and both report nothing left after.
func TestMigratePretend(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_pretend")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
alpha, beta := pendingPlugins()
plugins := []party.Plugin{alpha, beta}
before, err := PendingMigrations(gdb, plugins)
if err != nil {
t.Fatal(err)
}
out, errOut, code, left := runLagoon(t, "production", dsn, plugins, "migrate", "--pretend")
if code != bonfire.ExitOK || left != 2 {
t.Fatalf("--pretend: exit %d, stdin left %d\n%s%s", code, left, out, errOut)
}
for _, ref := range before {
if !strings.Contains(out, ref.Plugin+": "+ref.ID+"\n") {
t.Errorf("--pretend lacks %s: %s:\n%s", ref.Plugin, ref.ID, out)
}
}
if !strings.Contains(out, fmt.Sprintf("%d migrations would run", len(before))) {
t.Errorf("--pretend lacks the count:\n%s", out)
}
after, err := PendingMigrations(gdb, plugins)
if err != nil || len(after) != len(before) || gdb.Migrator().HasTable("pending_alpha") {
t.Fatalf("--pretend applied something: %v, %v", after, err)
}
// -q keeps the data lines (the IDs) and drops the count line.
out, _, code, _ = runLagoon(t, "production", dsn, plugins, "migrate", "--pretend", "-q")
if code != bonfire.ExitOK || !strings.Contains(out, "demo.beta: 202610210002_seed_pending_beta\n") || strings.Contains(out, "would run") {
t.Errorf("--pretend -q:\n%s", out)
}
out, errOut, code, left = runLagoon(t, "production", dsn, plugins, "migrate")
if code != bonfire.ExitOK || left != 2 || strings.Contains(out, "[y/N]") {
t.Fatalf("production migrate off a terminal: exit %d, stdin left %d\n%s%s", code, left, out, errOut)
}
if !strings.Contains(out, fmt.Sprintf("Applied %d migrations", len(before))) || !strings.Contains(out, "demo.beta: 2 applied") {
t.Errorf("migrate result lines:\n%s", out)
}
if !gdb.Migrator().HasTable("pending_beta") {
t.Fatal("migrate did not apply beta")
}
out, _, _, _ = runLagoon(t, "production", dsn, plugins, "migrate", "--pretend")
if !strings.Contains(out, "0 migrations would run") {
t.Errorf("--pretend after migrate:\n%s", out)
}
out, _, _, _ = runLagoon(t, "production", dsn, plugins, "migrate")
if !strings.Contains(out, "Nothing to migrate") {
t.Errorf("second migrate:\n%s", out)
}
}
// TestRollbackPretend: migrate:rollback --pretend names the migration that
// would roll back and changes nothing; the real rollback in production off
// a terminal proceeds without asking.
func TestRollbackPretend(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_rollback_pretend")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
alpha, beta := pendingPlugins()
plugins := []party.Plugin{alpha, beta}
if err := Migrate(gdb, []party.Plugin{alpha}); err != nil {
t.Fatal(err)
}
out, _, code, _ := runLagoon(t, "production", dsn, plugins, "migrate:rollback", "--plugin", "demo.beta", "--pretend")
if code != bonfire.ExitOK || !strings.Contains(out, "demo.beta has no applied migration to roll back") {
t.Fatalf("pretend without history: exit %d\n%s", code, out)
}
if err := Migrate(gdb, plugins); err != nil {
t.Fatal(err)
}
out, _, code, _ = runLagoon(t, "production", dsn, plugins, "migrate:rollback", "--plugin", "demo.beta", "--pretend")
if code != bonfire.ExitOK || !strings.Contains(out, "Would roll back 202610210002_seed_pending_beta of demo.beta") {
t.Fatalf("pretend: exit %d\n%s", code, out)
}
var n int64
if err := gdb.Table("summer_migrations_demo_beta").Count(&n).Error; err != nil || n != 2 {
t.Fatalf("pretend changed the history: %d rows, %v", n, err)
}
out, errOut, code, left := runLagoon(t, "production", dsn, plugins, "migrate:rollback", "--plugin", "demo.beta")
if code != bonfire.ExitOK || left != 2 || !strings.Contains(out, "Rolled back 202610210002_seed_pending_beta of demo.beta") {
t.Fatalf("rollback: exit %d, stdin left %d\n%s%s", code, left, out, errOut)
}
if err := gdb.Table("summer_migrations_demo_beta").Count(&n).Error; err != nil || n != 1 {
t.Fatalf("rollback left %d history rows, %v", n, err)
}
}
// TestDeferredPurgePretend: deferred:purge --pretend counts the bindings
// older than --days and deletes none; the real purge in production off a
// terminal proceeds without asking.
func TestDeferredPurgePretend(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_purge_pretend")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
if err := Migrate(gdb, nil); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-10 * 24 * time.Hour)
for i, created := range []time.Time{old, old, time.Now()} {
row := DeferredBinding{MasterType: "acme.blog.post", MasterField: "cover", SlaveType: "acme.unknown", SlaveID: fmt.Sprint(i + 1),
SessionKey: "session", IsBind: true, BackendUserID: 1, CreatedAt: created, UpdatedAt: created}
if err := gdb.Create(&row).Error; err != nil {
t.Fatal(err)
}
}
out, errOut, code, _ := runLagoon(t, "production", dsn, nil, "deferred:purge", "--days", "5", "--pretend")
if code != bonfire.ExitOK || !strings.Contains(out, "2 deferred bindings older than 5 days would be purged") {
t.Fatalf("pretend: exit %d\n%s%s", code, out, errOut)
}
var n int64
if err := gdb.Model(&DeferredBinding{}).Count(&n).Error; err != nil || n != 3 {
t.Fatalf("pretend deleted bindings: %d left, %v", n, err)
}
out, errOut, code, left := runLagoon(t, "production", dsn, nil, "deferred:purge", "--days", "5")
if code != bonfire.ExitOK || left != 2 || !strings.Contains(out, "older than 5 days") || strings.Contains(out, "[y/N]") {
t.Fatalf("purge: exit %d, stdin left %d\n%s%s", code, left, out, errOut)
}
}
// TestMigrateStatusJSON: migrate:status --json lists every declared
// migration of the plugins with its table and applied flag, under -q too.
func TestMigrateStatusJSON(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_status_json")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
alpha, beta := pendingPlugins()
plugins := []party.Plugin{alpha, beta}
if err := Migrate(gdb, []party.Plugin{alpha}); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"migrate:status", "--json"}, {"migrate:status", "--json", "-q"}} {
out, errOut, code, _ := runLagoon(t, "development", dsn, plugins, args...)
if code != bonfire.ExitOK {
t.Fatalf("%v: exit %d\n%s", args, code, errOut)
}
var list []statusJSON
if err := json.Unmarshal([]byte(out), &list); err != nil {
t.Fatalf("%v: not JSON: %v\n%s", args, err, out)
}
want := []statusJSON{
{Plugin: "demo.alpha", Table: "summer_migrations_demo_alpha", ID: "202610210001_create_pending_alpha", Applied: true},
{Plugin: "demo.beta", Table: "summer_migrations_demo_beta", ID: "202610210001_create_pending_beta"},
{Plugin: "demo.beta", Table: "summer_migrations_demo_beta", ID: "202610210002_seed_pending_beta"},
}
if fmt.Sprint(list) != fmt.Sprint(want) {
t.Errorf("%v: status = %+v, want %+v", args, list, want)
}
if !strings.Contains(out, `"applied": false`) {
t.Errorf("%v: pending entries lack \"applied\": false:\n%s", args, out)
}
}
}
// TestMigrateAppliedLines (WR-07): migrate's "N applied" lines count what
// Migrate applied, also when a later set fails: the sets before the failure
// are reported, the error is returned and no success line is printed. The
// next run reports only what it applied.
func TestMigrateAppliedLines(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_applied_lines")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
create := func(name, sql string) *gormigrate.Migration {
return &gormigrate.Migration{
ID: "202610220001_create_" + name,
Migrate: func(tx *gorm.DB) error { return tx.Exec(sql).Error },
Rollback: func(tx *gorm.DB) error { return tx.Exec(`DROP TABLE IF EXISTS ` + name).Error },
}
}
alpha := migPlugin{id: "demo.okay", migrations: []*gormigrate.Migration{create("applied_okay", `CREATE TABLE applied_okay (id BIGSERIAL PRIMARY KEY)`)}}
broken := migPlugin{id: "demo.broken", migrations: []*gormigrate.Migration{create("applied_broken", `CREATE TABLE applied_broken (id BOGUS_TYPE)`)}}
framework, err := PendingMigrations(gdb, nil)
if err != nil {
t.Fatal(err)
}
out, errOut, code, _ := runLagoon(t, "local", dsn, []party.Plugin{alpha, broken}, "migrate")
if code != bonfire.ExitFailure || !strings.Contains(errOut, "demo.broken") {
t.Fatalf("migrate with a failing set: exit %d\n%s%s", code, out, errOut)
}
if !strings.Contains(out, "demo.okay: 1 applied\n") {
t.Errorf("the set applied before the failure is not reported:\n%s", out)
}
seen := map[string]bool{}
for _, ref := range framework {
if !seen[ref.Plugin] && !strings.Contains(out, ref.Plugin+": ") {
t.Errorf("framework set %s not reported:\n%s", ref.Plugin, out)
}
seen[ref.Plugin] = true
}
if strings.Contains(out, "demo.broken: ") || strings.Contains(out, "Applied ") {
t.Errorf("a failed set or a success line reported:\n%s", out)
}
fixed := migPlugin{id: "demo.broken", migrations: []*gormigrate.Migration{create("applied_broken", `CREATE TABLE applied_broken (id BIGSERIAL PRIMARY KEY)`)}}
out, errOut, code, _ = runLagoon(t, "local", dsn, []party.Plugin{alpha, fixed}, "migrate")
if code != bonfire.ExitOK {
t.Fatalf("second migrate: exit %d\n%s%s", code, out, errOut)
}
if !strings.Contains(out, "demo.broken: 1 applied\n") || !strings.Contains(out, "Applied 1 migrations") || strings.Contains(out, "demo.okay: ") {
t.Errorf("second migrate result lines:\n%s", out)
}
out, _, _, _ = runLagoon(t, "local", dsn, []party.Plugin{alpha, fixed}, "migrate")
if !strings.Contains(out, "Nothing to migrate") {
t.Errorf("third migrate:\n%s", out)
}
}

View File

@@ -0,0 +1,152 @@
//go:build linux
package lagoon
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"sync"
"syscall"
"testing"
"time"
"unsafe"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
// ptyOutput collects what the program writes to a pseudo-terminal.
type ptyOutput struct {
mu sync.Mutex
buf bytes.Buffer
}
func (p *ptyOutput) String() string {
p.mu.Lock()
defer p.mu.Unlock()
return p.buf.String()
}
// openPTY opens a pseudo-terminal pair: the slave is a terminal for the
// console (stdin and stdout), input written to the master reaches the
// slave's stdin, and everything the slave prints is collected.
func openPTY(t *testing.T) (master, slave *os.File, output *ptyOutput) {
t.Helper()
m, err := os.OpenFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
t.Fatalf("open /dev/ptmx: %v", err)
}
var unlock int32
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, m.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&unlock))); e != 0 {
t.Fatalf("unlock pty: %v", e)
}
var n uint32
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, m.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); e != 0 {
t.Fatalf("pty number: %v", e)
}
s, err := os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
t.Fatalf("open pty slave: %v", err)
}
output = &ptyOutput{}
done := make(chan struct{})
go func() {
defer close(done)
buf := make([]byte, 4096)
for {
n, err := m.Read(buf)
output.mu.Lock()
output.buf.Write(buf[:n])
output.mu.Unlock()
if err != nil {
return
}
}
}()
t.Cleanup(func() {
_ = s.Close()
_ = m.Close()
<-done
})
return m, s, output
}
// waitFor waits until the pty output contains want.
func waitFor(t *testing.T, output *ptyOutput, want string) {
t.Helper()
for range 200 {
if strings.Contains(output.String(), want) {
return
}
<-time.After(10 * time.Millisecond)
}
t.Fatalf("pty output lacks %q:\n%s", want, output.String())
}
// runOnPTY runs probe:confirm with args on a fresh pseudo-terminal after
// typing answer, and returns the exit code and the terminal output.
func runOnPTY(t *testing.T, env, answer string, args ...string) (int, *ptyOutput) {
t.Helper()
master, slave, output := openPTY(t)
if _, err := master.Write([]byte(answer)); err != nil {
t.Fatal(err)
}
root, err := bonfire.NewRootIO("acme", []bonfire.Command{probeConfirm(envApp(t, env, ""))}, slave, slave, slave)
if err != nil {
t.Fatal(err)
}
// --no-ansi keeps the line prompt (huh needs a terminal size).
code := bonfire.Execute(context.Background(), root, append([]string{"--no-ansi", "probe:confirm"}, args...))
return code, output
}
// TestConfirmProductionInteractiveNo: on an interactive terminal in
// production the question is asked; "n" or an empty answer prints the
// cancel line and exits 1 without an error block (Reported); "y" proceeds.
func TestConfirmProductionInteractiveNo(t *testing.T) {
for _, answer := range []string{"n\n", "\n", "no\n"} {
code, output := runOnPTY(t, "production", answer)
if code != bonfire.ExitFailure {
t.Fatalf("answer %q: exit %d\n%s", answer, code, output.String())
}
waitFor(t, output, "Migration cancelled.")
got := output.String()
if !strings.Contains(got, "Apply 3 migrations to the production database? [y/N]") {
t.Errorf("answer %q: question not asked:\n%s", answer, got)
}
if strings.Contains(got, "ERROR") || strings.Contains(got, "proceeded") {
t.Errorf("answer %q: error block or the step ran:\n%s", answer, got)
}
}
code, output := runOnPTY(t, "production", "y\n")
if code != bonfire.ExitOK {
t.Fatalf("answer y: exit %d\n%s", code, output.String())
}
waitFor(t, output, "proceeded")
}
// TestForceSkipsConfirm: --force, -n and any environment other than
// production proceed on an interactive terminal without asking (the typed
// "n" is never read).
func TestForceSkipsConfirm(t *testing.T) {
for name, tc := range map[string]struct {
env string
args []string
}{
"--force": {"production", []string{"--force"}},
"-n": {"production", []string{"-n"}},
"staging": {"staging", nil},
"development": {"development", nil},
} {
code, output := runOnPTY(t, tc.env, "n\n", tc.args...)
if code != bonfire.ExitOK {
t.Fatalf("%s: exit %d\n%s", name, code, output.String())
}
waitFor(t, output, "proceeded")
if strings.Contains(output.String(), "[y/N]") {
t.Errorf("%s: the question was asked:\n%s", name, output.String())
}
}
}

View File

@@ -142,3 +142,42 @@ func TestInstallDefault(t *testing.T) {
t.Fatalf("default logger output:\n%s", out)
}
}
// TestInstallDefaultUnchanged: InstallDefault, the compatibility entry
// point that generated applications no longer call, still installs a
// redacting logfmt TextHandler on the given writer, and each call replaces
// the previous default instead of wrapping it.
func TestInstallDefaultUnchanged(t *testing.T) {
prev := slog.Default()
prevFlags, prevWriter := log.Flags(), log.Writer()
t.Cleanup(func() {
slog.SetDefault(prev)
log.SetFlags(prevFlags)
log.SetOutput(prevWriter)
})
var first, second bytes.Buffer
InstallDefault(&first)
InstallDefault(&second)
slog.Debug("below the default level", "token", nested)
slog.Info("payment for Bearer "+bearer, "api_key", apiKey, slog.Group("g", "Password", passwd))
if first.Len() != 0 {
t.Fatalf("the replaced default still receives records:\n%s", first.String())
}
out := second.String()
if strings.Count(out, "\n") != 1 {
t.Fatalf("want exactly one line (info level, debug dropped):\n%s", out)
}
for _, want := range []string{"time=", " level=INFO ", `msg="payment for Bearer [REDACTED]"`, "api_key=[REDACTED]", "g.Password=[REDACTED]"} {
if !strings.Contains(out, want) {
t.Errorf("default output lacks %q:\n%s", want, out)
}
}
for _, secret := range []string{bearer, apiKey, passwd, nested} {
if strings.Contains(out, secret) {
t.Errorf("default output leaks %q:\n%s", secret, out)
}
}
if strings.HasPrefix(strings.TrimSpace(out), "{") {
t.Errorf("default output is JSON, want logfmt:\n%s", out)
}
}

View File

@@ -4,14 +4,19 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/diary"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/party"
)
@@ -413,3 +418,167 @@ func TestAccessLog(t *testing.T) {
t.Fatalf("direct records = %v", recs)
}
}
// sharePlugin serves token-bearing paths: GET /share/{token} and
// POST /invite/{token}.
type sharePlugin struct{}
func (sharePlugin) ID() string { return "acme.share" }
func (sharePlugin) Requires() []string { return nil }
func (sharePlugin) Register(*backpack.App) error { return nil }
func (sharePlugin) Boot(*backpack.App) error { return nil }
func (sharePlugin) Routes(r pact.Router) error {
r.Get("/share/{token}", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok")) })
r.Post("/invite/{token}", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
return nil
}
// diaryAccessRouter assembles plugin with httpYAML and no published
// logger, so the request log goes to the diary component http, set up on a
// console buffer with environ.
func diaryAccessRouter(t *testing.T, httpYAML string, plugin party.Plugin, environ ...string) (http.Handler, *syncBuffer) {
t.Helper()
app := backpack.New(writeHTTPConfig(t, "body_limits:\n default_bytes: 1024\n upload_bytes: 1024\n"+httpYAML))
h, err := Assemble(app, []party.Plugin{plugin})
if err != nil {
t.Fatal(err)
}
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Environ: append([]string{"SUMMER_LOG__DEFAULT=console"}, environ...)})
if err != nil {
t.Fatal(err)
}
var buf syncBuffer
t.Cleanup(diary.Reset)
if err := diary.Setup(cfg, diary.Options{Stderr: &buf, Root: t.TempDir(), Environ: []string{}}); err != nil {
t.Fatal(err)
}
return h, &buf
}
// TestAccessLogRequestIDCap: X-Request-Id is trimmed, cut to 128 bytes on
// a rune boundary and stripped of invalid UTF-8.
func TestAccessLogRequestIDCap(t *testing.T) {
h, buf := accessLogRouter(t, true)
cases := map[string]string{
strings.Repeat("a", 300): strings.Repeat("a", errorPageMaxRequestID),
"\t req-7 \t": "req-7",
strings.Repeat("a", 127) + "é" + "tail": strings.Repeat("a", 127),
"bad\xffid": "badid",
}
for header, want := range cases {
buf.Reset()
req := httptest.NewRequest(http.MethodGet, "/blog/posts/1", nil)
req.Header["X-Request-Id"] = []string{header}
h.ServeHTTP(httptest.NewRecorder(), req)
recs := accessRecords(t, buf)
if len(recs) != 1 || recs[0]["request_id"] != want {
t.Errorf("X-Request-Id %q: records %v, want request_id %q", header, recs, want)
}
if id, _ := recs[0]["request_id"].(string); len(id) > errorPageMaxRequestID {
t.Errorf("request_id is %d bytes", len(id))
}
}
buf.Reset()
req := httptest.NewRequest(http.MethodGet, "/blog/posts/1", nil)
req.Header.Set("X-Request-Id", " ")
h.ServeHTTP(httptest.NewRecorder(), req)
if recs := accessRecords(t, buf); len(recs) != 1 || recs[0]["request_id"] != nil {
t.Errorf("a blank X-Request-Id gives %v, want no request_id", recs)
}
}
// TestAccessLogDefaultOn: without http.access_log, and with it true, every
// request is logged once.
func TestAccessLogDefaultOn(t *testing.T) {
for name, yaml := range map[string]string{"unset": "", "true": "access_log: true\n"} {
h, buf := diaryAccessRouter(t, yaml, blogPostsPlugin{})
for range 3 {
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/blog/posts/7", nil))
}
if n := strings.Count(buf.String(), `msg="http request"`); n != 3 {
t.Errorf("%s: %d request records, want 3:\n%s", name, n, buf.String())
}
}
}
// TestAccessLogDisabled: http.access_log false, also as the string an
// environment variable gives, turns the request log off.
func TestAccessLogDisabled(t *testing.T) {
h, buf := diaryAccessRouter(t, "access_log: false\n", blogPostsPlugin{})
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/blog/posts/7", nil))
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/nowhere", nil))
if strings.Contains(buf.String(), "http request") {
t.Errorf("access_log false still logged:\n%s", buf.String())
}
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "http.yaml"), []byte("body_limits:\n default_bytes: 1024\n upload_bytes: 1024\n"), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := compass.Open(compass.Options{Dir: dir, Environ: []string{"SUMMER_ENV=development", "SUMMER_HTTP__ACCESS_LOG=false"}})
if err != nil {
t.Fatal(err)
}
if accessLogEnabled(backpack.New(cfg)) {
t.Error("SUMMER_HTTP__ACCESS_LOG=false does not turn the request log off")
}
if !accessLogEnabled(nil) || !accessLogEnabled(backpack.New(nil)) {
t.Error("the request log is off without a config")
}
}
// TestAccessLogNeverLogsPath: token-bearing paths and queries never reach
// the request log, for a matched route, a 404 (trailing slash) and a 405
// (wrong method), on the default diary console.
func TestAccessLogNeverLogsPath(t *testing.T) {
h, buf := diaryAccessRouter(t, "", sharePlugin{})
cases := []struct {
method, target string
status int
pattern string
}{
{http.MethodGet, "/share/tok-secret-200?sig=query-secret", http.StatusOK, `pattern="GET /share/{token}"`},
{http.MethodGet, "/share/tok-secret-404/?sig=query-secret", http.StatusNotFound, `pattern=""`},
{http.MethodDelete, "/share/tok-secret-405?sig=query-secret", http.StatusMethodNotAllowed, `pattern=""`},
{http.MethodGet, "/invite/tok-secret-invite", http.StatusMethodNotAllowed, `pattern=""`},
{http.MethodPost, "/invite/tok-secret-post", http.StatusNoContent, `pattern="POST /invite/{token}"`},
}
for _, tc := range cases {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(tc.method, tc.target, nil))
if rec.Code != tc.status {
t.Fatalf("%s %s = %d, want %d", tc.method, tc.target, rec.Code, tc.status)
}
}
got := buf.String()
lines := strings.Split(strings.TrimSpace(got), "\n")
if len(lines) != len(cases) {
t.Fatalf("want %d request records, got %d:\n%s", len(cases), len(lines), got)
}
for i, tc := range cases {
if !strings.Contains(lines[i], tc.pattern) || !strings.Contains(lines[i], fmt.Sprintf("status=%d", tc.status)) {
t.Errorf("record %d = %q, want %s status=%d", i, lines[i], tc.pattern, tc.status)
}
}
for _, leak := range []string{"tok-secret", "query-secret", "sig=", "/share/tok", "/invite/tok"} {
if strings.Contains(got, leak) {
t.Errorf("the request log carries %q:\n%s", leak, got)
}
}
}
// TestAccessLogComponentHTTP: without a published logger the request log
// is the diary component http, so log.components.http: warn silences it.
func TestAccessLogComponentHTTP(t *testing.T) {
h, buf := diaryAccessRouter(t, "", blogPostsPlugin{})
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/blog/posts/7", nil))
if !strings.Contains(buf.String(), "component=http") || !strings.Contains(buf.String(), "level=INFO") {
t.Errorf("request record lacks component=http:\n%s", buf.String())
}
quiet, qbuf := diaryAccessRouter(t, "", blogPostsPlugin{}, "SUMMER_LOG__COMPONENTS__HTTP=warn")
quiet.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/blog/posts/7", nil))
if strings.Contains(qbuf.String(), "http request") {
t.Errorf("log.components.http warn still logged:\n%s", qbuf.String())
}
}

View File

@@ -0,0 +1,352 @@
package surf
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/diary"
"git.golem15.com/golem15/summercms/modules/lagoon"
"git.golem15.com/golem15/summercms/modules/party"
)
// summaryFixture is buildFacts over summaryApp with two plugins, three
// pending migrations and a worker.
func summaryFixture(t *testing.T) summaryFacts {
t.Helper()
return buildFacts(summaryApp(t), factInputs{
addr: ":8096",
bin: "bin/acme",
plugins: []party.Plugin{summaryPlugin{id: "acme.user"}, summaryPlugin{id: "acme.blog", req: []string{"acme.user"}}},
queues: []string{"default"},
worker: true,
adminPrefix: "/backend",
mounted: true,
admins: adminsPresent,
pending: []lagoon.MigrationRef{
{Plugin: "acme.blog", ID: "0001_posts"},
{Plugin: "acme.blog", ID: "0002_tags"},
{Plugin: "summercms.cabana", ID: "0001"},
},
version: bonfire.Version{App: "v1.4.0", Framework: "v0.4.0"},
})
}
// runSummaryProbe prints the summary through a command run with args and
// returns its output.
func runSummaryProbe(t *testing.T, f summaryFacts, args ...string) string {
t.Helper()
probe := []bonfire.Command{{Name: "probe:summary", Run: func(_ context.Context, _ bonfire.Input, out bonfire.Output) error {
printSummary(out, f)
return nil
}}}
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), probe, "probe:summary", args, &buf); err != nil {
t.Fatal(err)
}
return buf.String()
}
// TestServeSummaryLines: the block at verbosity 0, the per-plugin lines at
// -v, nothing under -q, and never a DSN user or password; the database
// label for URL, keyword, socket, IPv6, empty and unparsable DSNs.
func TestServeSummaryLines(t *testing.T) {
f := summaryFixture(t)
plain := runSummaryProbe(t, f)
for _, want := range []string{
"SummerCMS acme v1.4.0 (framework v0.4.0)\n",
" Environment staging\n",
" URL http://127.0.0.1:8096/\n",
" Admin http://127.0.0.1:8096/backend\n",
" Database db.internal:5433/acme_prod\n",
" Migrations 3 pending — run bin/acme migrate\n",
" Plugins 2 (acme.user, acme.blog)\n",
" Queue default (worker in serve)\n",
" Scheduler in serve\n",
} {
if !strings.Contains(plain, want) {
t.Errorf("verbosity 0 block lacks %q:\n%s", want, plain)
}
}
if strings.Contains(plain, "requires") || strings.Contains(plain, "sign in") {
t.Errorf("verbosity 0 block has per-plugin lines or an admin note with admins present:\n%s", plain)
}
verbose := runSummaryProbe(t, f, "-v")
for _, want := range []string{
" acme.user requires nothing; 0 pending migrations\n",
" acme.blog requires acme.user; 2 pending migrations\n",
} {
if !strings.Contains(verbose, want) {
t.Errorf("-v block lacks %q:\n%s", want, verbose)
}
}
if quiet := runSummaryProbe(t, f, "-q"); quiet != "" {
t.Errorf("-q printed the block:\n%s", quiet)
}
for _, out := range []string{plain, verbose} {
for _, secret := range []string{"summer_user", "pa55-secret", "postgres://"} {
if strings.Contains(out, secret) {
t.Fatalf("summary leaks %q:\n%s", secret, out)
}
}
}
// The worker off, migrations up to date or unknown.
off := buildFacts(summaryApp(t), factInputs{addr: "127.0.0.1:80", bin: "bin/acme", version: bonfire.Version{App: "devel"}})
if off.URL != "http://127.0.0.1/" || !strings.Contains(off.Queue, "queue.work_in_serve is false") || !strings.Contains(off.Scheduler, "run schedule:run") {
t.Errorf("worker off facts = %+v", off)
}
if m, warn := off.migrations(); m != "up to date" || warn {
t.Errorf("no pending migrations = %q, %v", m, warn)
}
unknown := buildFacts(summaryApp(t), factInputs{addr: ":1", pendingErr: errors.New("no history")})
if m, warn := unknown.migrations(); !strings.HasPrefix(m, "unknown") || !warn || unknown.Pending != -1 {
t.Errorf("unreadable migrations = %q, %v, %d", m, warn, unknown.Pending)
}
for dsn, want := range map[string]string{
"postgres://u:secret@db.example:5432/app": "db.example:5432/app",
"host=db port=6543 dbname=acme user=u password=p": "db:6543/acme",
"host=/var/run/postgresql dbname=acme user=u": "socket /var/run/postgresql:5432/acme",
"postgres://u:p@[::1]:5432/acme": "[::1]:5432/acme",
"": "not configured",
"postgres://u:p@[::1:bad": "configured",
} {
if got := lagoon.DatabaseLabel(dsn); got != want {
t.Errorf("DatabaseLabel(%q) = %q, want %q", dsn, got, want)
}
}
}
// TestServeRecords: off a terminal serve logs one "startup summary" record
// with every fact and one record whose message is exactly "listening on
// <addr>"; with HideOnTTY (used only when the block printed) a styled
// console skips both while the daily file keeps them.
func TestServeRecords(t *testing.T) {
f := summaryFixture(t)
var logs bytes.Buffer
logSummary(context.Background(), slog.New(slog.NewJSONHandler(&logs, nil)), f)
var summary map[string]any
var messages []string
for _, line := range strings.Split(strings.TrimSpace(logs.String()), "\n") {
var rec map[string]any
if err := json.Unmarshal([]byte(line), &rec); err != nil {
t.Fatal(err)
}
messages = append(messages, rec["msg"].(string))
if rec["msg"] == summaryMessage {
summary = rec
}
}
if strings.Join(messages, "|") != "startup summary|listening on :8096" {
t.Fatalf("records = %q, want the summary and the listening line (no admin note with admins present)", messages)
}
keys := []string{"app", "environment", "url", "admin", "database", "migrations_pending", "plugins", "plugin_ids", "queue", "scheduler", "mail", "cache", "realtime", "search"}
for _, k := range keys {
if _, ok := summary[k]; !ok {
t.Errorf("summary record lacks %s: %v", k, summary)
}
}
if summary["app"] != "SummerCMS acme v1.4.0 (framework v0.4.0)" || summary["url"] != "http://127.0.0.1:8096/" {
t.Errorf("summary record = %v", summary)
}
// Admin notes follow, warnings at WARN.
logs.Reset()
noAdmins := f
noAdmins.Admin, noAdmins.AdminNotes = adminStatus(f.URL, "/backend", true, adminsNone)
logSummary(context.Background(), slog.New(slog.NewJSONHandler(&logs, nil)), noAdmins)
if !strings.Contains(logs.String(), `"level":"WARN","msg":"no administrators yet`) {
t.Errorf("admin warning record missing:\n%s", logs.String())
}
// HideOnTTY: hidden on a styled console, kept in the daily file.
root := t.TempDir()
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Environ: []string{"SUMMER_LOG__CHANNELS__DAILY__LEVEL=info"}})
if err != nil {
t.Fatal(err)
}
var console syncBuffer
t.Cleanup(diary.Reset)
now := func() time.Time { return time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) }
if err := diary.Setup(cfg, diary.Options{Stderr: &console, Root: root, Color: bonfire.ColorAlways, Environ: []string{}, Now: now}); err != nil {
t.Fatal(err)
}
logSummary(diary.HideOnTTY(context.Background()), serveLog, f)
if strings.Contains(console.String(), summaryMessage) || strings.Contains(console.String(), "listening on") {
t.Errorf("styled console shows the hidden records:\n%s", console.String())
}
logSummary(context.Background(), serveLog, f)
if !strings.Contains(console.String(), "listening on :8096") {
t.Errorf("styled console lacks the records logged without HideOnTTY:\n%s", console.String())
}
if err := diary.Close(); err != nil {
t.Fatal(err)
}
daily, err := os.ReadFile(filepath.Join(root, "storage", "logs", "summer-2026-10-21.log"))
if err != nil {
t.Fatal(err)
}
if n := strings.Count(string(daily), `"msg":"listening on :8096"`); n != 2 {
t.Errorf("daily file has %d listening records, want 2 (hidden and shown):\n%s", n, daily)
}
}
// shutdownServer serves handler through a requestCounter on a loopback
// listener and returns the server, the counter and the base URL.
func shutdownServer(t *testing.T, handler http.Handler) (*http.Server, *requestCounter, chan error, string) {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
counter := newRequestCounter(handler)
srv := &http.Server{Handler: counter}
errCh := make(chan error, 1)
go func() { errCh <- srv.Serve(ln) }()
return srv, counter, errCh, "http://" + ln.Addr().String()
}
// TestShutdownReportAborted: a request still running at the drain deadline
// is counted as aborted, the drain error is in the report and Err returns
// it, which makes serve exit 1.
func TestShutdownReportAborted(t *testing.T) {
started, release := make(chan struct{}), make(chan struct{})
srv, counter, errCh, base := shutdownServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
close(started)
<-release
w.WriteHeader(http.StatusOK)
}))
defer close(release)
go func() {
if resp, err := http.Get(base + "/slow"); err == nil {
_ = resp.Body.Close()
}
}()
<-started
report := shutdownReport{Draining: counter.inFlight.Load()}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
report.Aborted = counter.inFlight.Load()
_ = srv.Close()
report.Errors = append(report.Errors, fmt.Errorf("surf: drain requests: %w", err))
}
if err := <-errCh; err != http.ErrServerClosed {
t.Fatalf("Serve returned %v", err)
}
report.Served = counter.served.Load()
if report.Draining != 1 || report.Aborted != 1 || report.Drained() != 0 {
t.Fatalf("report = %+v, want 1 draining, 1 aborted, 0 drained", report)
}
if err := report.Err(); err == nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Err() = %v, want the drain deadline error", err)
}
var buf bytes.Buffer
bonfire.Details(bonfire.NewOutput(strings.NewReader(""), &buf, &buf), "Shutdown report", report.details())
for _, want := range []string{"0 drained, 1 aborted", "surf: drain requests: context deadline exceeded"} {
if !strings.Contains(buf.String(), want) {
t.Errorf("report lacks %q:\n%s", want, buf.String())
}
}
}
// TestServeGracefulNil: requests that finish within the deadline drain,
// the report counts them as served and drained, and Err is nil (exit 0).
func TestServeGracefulNil(t *testing.T) {
started, release := make(chan struct{}), make(chan struct{})
srv, counter, errCh, base := shutdownServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/slow" {
close(started)
<-release
}
w.WriteHeader(http.StatusOK)
}))
for range 3 {
resp, err := http.Get(base + "/fast")
if err != nil {
t.Fatal(err)
}
_ = resp.Body.Close()
}
slowDone := make(chan int, 1)
go func() {
resp, err := http.Get(base + "/slow")
if err != nil {
slowDone <- 0
return
}
_ = resp.Body.Close()
slowDone <- resp.StatusCode
}()
<-started
report := shutdownReport{Draining: counter.inFlight.Load()}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
shut := make(chan error, 1)
go func() { shut <- srv.Shutdown(ctx) }()
close(release)
if err := <-shut; err != nil {
t.Fatalf("Shutdown: %v", err)
}
if err := <-errCh; err != http.ErrServerClosed {
t.Fatalf("Serve returned %v", err)
}
if code := <-slowDone; code != http.StatusOK {
t.Fatalf("the in-flight request got %d, want 200", code)
}
report.Served = counter.served.Load()
if report.Draining != 1 || report.Aborted != 0 || report.Drained() != 1 || report.Served != 4 {
t.Fatalf("report = %+v, want 1 draining, 1 drained, 4 served", report)
}
if err := report.Err(); err != nil {
t.Fatalf("Err() = %v, want nil after a graceful stop", err)
}
}
// TestRouteListJSON: route:list --json prints every route as a JSON array
// with method, pattern, plugin, middleware (never null) and raw, under -q
// too; the table and the admin line are left out.
func TestRouteListJSON(t *testing.T) {
app := backpack.New(writeHTTPConfig(t, "body_limits:\n default_bytes: 1024\n upload_bytes: 1024\n"))
cmd := RouteListCommand(app, []party.Plugin{blogPostsPlugin{}})
for _, args := range [][]string{{"--json"}, {"--json", "-q"}} {
var buf bytes.Buffer
if err := bonfire.Call(context.Background(), []bonfire.Command{cmd}, "route:list", args, &buf); err != nil {
t.Fatal(err)
}
var routes []map[string]any
if err := json.Unmarshal(buf.Bytes(), &routes); err != nil {
t.Fatalf("%v: not a JSON array: %v\n%s", args, err, buf.String())
}
if len(routes) != 1 {
t.Fatalf("%v: routes = %v", args, routes)
}
r := routes[0]
if r["method"] != "GET" || r["pattern"] != "/blog/posts/{id}" || r["plugin"] != "acme.blog" || r["raw"] != false {
t.Errorf("%v: route = %v", args, r)
}
if mw, ok := r["middleware"].([]any); !ok || mw == nil {
t.Errorf("%v: middleware is not an array: %v", args, r["middleware"])
}
if strings.Contains(buf.String(), "admin:") {
t.Errorf("%v: JSON output carries the admin line:\n%s", args, buf.String())
}
}
}

899
scripts/check-phase19.8.sh Executable file
View File

@@ -0,0 +1,899 @@
#!/usr/bin/env bash
# check-phase19.8: fail-closed gate for Phase 19.8, the console commands
# overhaul. It proves the phase across this framework (diary, bonfire and the
# swept framework commands), the example application, the docs, every shared
# plugin swept in plan 03, every application that runs the new console, the
# landing site and the Docker-backed paths.
#
# Stages:
# self-test the go test -json and node --test (TAP) detectors on planted
# output (pass, fail, skip, a subtest-only pass, zero tests, "no
# tests to run", a missing required name), the additive check
# and the NOT SWEPT rule on planted git repositories, the docker
# NOT RUN rule on a faked PATH, and that every mode is wired.
# go go vet ./... and go test -short ./... here, the required
# smoke and unit tests by name per package, then go test -race
# -short of diary, sunscreen, bonfire and docsite.
# hello the example application (its own module in the workspace):
# vet, every test, and its console tests by name.
# docs the console decision note names the six charm modules,
# TestDocsTree, summer docs:build --check, no consuming
# application named in the console and logging sources and
# pages, and every command in summer's JSON listing has
# examples.
# plugins each swept shared plugin (PLUGINS_ROOT, and the user, golem
# and feedback checkouts under FONOTEKA): vet, short tests, its
# warn-only TestCommandsDocumented by name, a build without the
# workspace (GOWORK=off), and the additive check: in the diff of
# its sweep commit (found by subject) against its parent no
# removed line declares an exported func, method or type, and
# no removed Name: "<x>" literal (a command, flag or argument
# name) is missing from the added lines. A repository listed in
# SWEEP_OPTIONAL without a sweep commit prints NOT SWEPT; any
# other one without it fails. Only the short suites run here:
# the Docker suites of sitemap, faq, menu and the user plugin's
# updates package fail before the sweep too (deferred-items.md)
# and are not part of this gate.
# apps each application: go build ./... (binaries into a temporary
# directory), summer build (this checkout's summer) leaves the
# committed main.go and plugins.gen.go unchanged, and
# bin/<binary> list and help migrate exit 0.
# landing the landing site: nuxt generate, then its node --test suite
# (TAP: every test passes, none skipped or todo); the
# application's terminal Go tests by name (the end-to-end
# TestTerminalCommands only with SUMMERCMS_TERMINAL_CHECK=1,
# NOT RUN otherwise); the demo
# application's roadmap tests by name and
# scripts/sync-roadmap.sh --check against ROADMAP_REF (default
# origin/master: local master carries other sessions' unpushed
# planning commits).
# docker Docker host only: the testcontainers tests by name without
# -short (serve's lifecycle, the migration checklist flags, the
# admin token JSON).
#
# A Go stage exits non-zero on a failing command, or on a go test -json run
# that fails, skips a test (a subtest included), matches zero tests, prints
# "no tests to run" or misses a required name (a subtest pass never counts
# for its parent's name).
#
# --all runs self-test, go, hello, docs, plugins, apps and landing, then
# docker when a Docker daemon answers (NOT RUN docker otherwise, a failure
# with PHASE198_REQUIRE_DOCKER=1).
#
# Environment overrides (default: the sibling checkouts next to this
# repository): PHASE198_ROOT (this repository), PLUGINS_ROOT (the directory
# holding the sm-*-plugin checkouts), FONOTEKA (the application workspace
# that carries the user, golem and feedback plugin checkouts), DEMO_APP,
# BM_APP, GRZYBY_APP, SUMMERCMSIO_APP, ROADMAP_REF, PHASE198_REQUIRE_DOCKER.
set -euo pipefail
unset FORCE_COLOR
export NO_COLOR=1
ROOT="${PHASE198_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
PLUGINS_ROOT="${PLUGINS_ROOT:-$ROOT/..}"
FONOTEKA="${FONOTEKA:-$ROOT/../fono""teka.go}"
DEMO_APP="${DEMO_APP:-$ROOT/../sm-demo-app}"
BM_APP="${BM_APP:-$ROOT/../sm-bm-app}"
GRZYBY_APP="${GRZYBY_APP:-$ROOT/../sm-grzybyfunkcjonalne-app}"
SUMMERCMSIO_APP="${SUMMERCMSIO_APP:-$ROOT/../sm-summercmsio-app}"
# ---------------------------------------------------------------------------
# Required test names. Each list runs with -run '^(a|b|...)$' and every name
# must pass; a name that skips or does not run fails the stage.
# go stage (-short).
DIARY_TESTS=(
# Plan 01.
TestDiaryTracerSmoke TestConfigEnvSmoke TestDailyDegradesSmoke
# Plan 04: config rules, floors, verbosity, quiet (D-08, D-09).
TestConfigRules TestConfigEnvStrings TestDefaultConfig
TestComponentFloors TestVerbosityLevels TestQuietConsoleOnly
# Plan 04: daily files (D-10).
TestDailyRotation TestDailyRetention TestDailyPermissions
TestDailyUnwritable TestDailyLazyOpen TestPathConfinement
# Plan 04: redaction, the console handler, lifecycle, concurrency (D-12).
TestRedactionEveryDriver TestConsoleStripsControlChars
TestConsoleComponentPrefix TestConsoleStyledOnlyOnTerminal
TestConsoleLevelFromOptions TestForBeforeSetup TestResetRestoresDefault
TestNoRecursionWithoutState TestHideOnTTY TestConcurrentWrites
# Plan 04: testing/slogtest conformance.
TestSlogtestConsoleHandler TestSlogtestPipeline
# Plan 05 (review fixes): daily retry, retired writers (WR-04, WR-06).
TestDailyRetriesAfterFailure TestRetiredDailyWriterKeepsNoFile
TestApplyGlobalsRebuildsOnce
)
SUNSCREEN_TESTS=(
# Plan 04: the compatibility entry point.
TestInstallDefault TestInstallDefaultUnchanged
)
BONFIRE_TESTS=(
# Plan 02.
TestListTracerSmoke TestHelpSmoke TestListJSONSmoke TestErrorRenderSmoke
TestWidgetMatrixSmoke
# Plan 04: global options, duplicates, Call (SC-2, D-13).
TestPeekGlobals TestGlobalsArgs TestGlobalFlagsAgree
TestReservedFlags TestDuplicateNames TestCallNoGlobalsHook
# Plan 04: goldens for list, help and errors (D-13, D-14, D-16, D-18).
TestListGolden TestListAllGolden TestListJSONGolden TestListStyledGolden
TestHelpGolden TestHelpPassthroughGolden TestHelpEqualsHelpFlag
TestErrorGolden TestExitCodes TestReportedSilent TestErrorScrubbed
TestWrongDirectoryHint TestCheckDocs TestVersionHeader
# Plan 04: widgets, live views and prompts (D-03, D-27, D-28).
TestWidgetMatrix TestLiveModels TestPromptsNoInteraction
TestSecretWithNoInteraction TestConfirmProceed TestPrintAbove
# Plan 05 (review fixes): live views (CR-01, WR-01, WR-02), exit codes
# (CR-02), the masked hint (WR-08), control characters (WR-05).
TestPrintAboveOrderedNoLoss TestPrintlnStaysWithItsConsole
TestLiveViewSingleOwner TestLiveViewPanicUnregisters
TestLiveViewRestoresTerminalOnSignal TestWithExitCode
TestWrongDirectoryMasksSecrets TestSanitize TestRenderErrorStripsControl
TestProcessViewStripsControl TestTableStripsControl
)
DOCSITE_TESTS=(
# Plan 04: the docs checker reads past the global options.
TestCommandWord TestCommandWordGlobals
)
CONGA_TESTS=(
# Plan 01.
TestRiverLoggerUsesDiarySmoke
# Plan 04 (D-09).
TestRiverLoggerComponent TestRiverLoggerPublishedOverride
# Plan 04: worker counters and queue:clear's production question
# (D-28, D-29).
TestWorkerStats TestQueueClearConfirm
)
LAGOON_TESTS=(
# Plan 01.
TestGormLoggerUsesDiarySmoke
# Plan 04 (D-09).
TestGormLoggerLevels
# Plan 04: the production question and key:generate (D-21, D-25, D-28).
TestConfirmProductionNonTTYProceeds TestConfirmProductionInteractiveNo
TestForceSkipsConfirm TestKeyGenerateJSON TestKeyGenerateQuiet
)
SURF_TESTS=(
# Plan 03.
TestServeSummarySmoke TestShutdownReportSmoke TestAccessLogSmoke
# Plan 04: summary, records, report, graceful stop, route:list --json
# (D-04, D-25, D-29).
TestServeSummaryLines TestServeRecords TestShutdownReportAborted
TestServeGracefulNil TestRouteListJSON
# Plan 04: the request log (D-23).
TestAccessLogRequestIDCap TestAccessLogDefaultOn TestAccessLogDisabled
TestAccessLogNeverLogsPath TestAccessLogComponentHTTP
)
FLARE_TESTS=(
# Plan 04: Reported failures (D-18).
TestFlareReported
)
DEV_TESTS=(
# Plan 04: summer dev runs serve (D-29).
TestStartBinServe TestDevReport TestDevGracefulNil
# Plan 05 (review fixes): serve's full shutdown grace (WR-03).
TestDevStopWaitsForGracefulShutdown
)
SUMMER_TESTS=(
# Plan 03.
TestCommandsDocumented TestDocsTree
# Plan 04: the delegates' help (D-17).
TestDelegateExamples
# Plan 05 (review fixes): delegate exit codes and streams (CR-02, CR-03).
TestDelegateExitCodePassthrough TestAppStreams TestDelegateStreams
)
# hello stage.
HELLO_TESTS=(
TestBuiltBinaryListsCommands TestGreeterDocumented TestHelpListsServe
TestBuiltBinaryUnknownCommandExitsNonZero TestMigrateWithoutDSNFailsLoudly
)
# docker stage: testcontainers suites (Postgres).
DOCKER_SURF_TESTS=(TestServeLifecycle)
DOCKER_LAGOON_TESTS=(
TestPendingMigrations TestMigratePretend TestRollbackPretend
TestDeferredPurgePretend TestMigrateStatusJSON
# Plan 05 (review fixes): applied lines from one set list (WR-07).
TestMigrateAppliedLines
)
DOCKER_CABANA_TESTS=(TestTokenCreateJSON TestTokenListJSON TestTokenCreateQuietKeepsToken)
# plugins stage: the swept repositories (19.8-03-SUMMARY.md, D-20), as
# "<label>|<path>".
PLUGIN_REPOS=(
"sm-paymentgateway-plugin|$PLUGINS_ROOT/sm-paymentgateway-plugin"
"sm-pgstripe-plugin|$PLUGINS_ROOT/sm-pgstripe-plugin"
"sm-oauthserver-plugin|$PLUGINS_ROOT/sm-oauthserver-plugin"
"sm-journal-plugin|$PLUGINS_ROOT/sm-journal-plugin"
"sm-sitemap-plugin|$PLUGINS_ROOT/sm-sitemap-plugin"
"sm-faq-plugin|$PLUGINS_ROOT/sm-faq-plugin"
"sm-menu-plugin|$PLUGINS_ROOT/sm-menu-plugin"
"sm-chat-plugin|$PLUGINS_ROOT/sm-chat-plugin"
"sm-chatchannels-plugin|$PLUGINS_ROOT/sm-chatchannels-plugin"
"sm-chatvideo-plugin|$PLUGINS_ROOT/sm-chatvideo-plugin"
"sm-forum-plugin|$PLUGINS_ROOT/sm-forum-plugin"
"sm-herald-plugin|$PLUGINS_ROOT/sm-herald-plugin"
"sm-golem-plugin|$FONOTEKA/plugins/golem15/golem"
"sm-feedback-plugin|$FONOTEKA/plugins/golem15/feedback"
"sm-user-plugin|$FONOTEKA/plugins/golem15/user"
)
# Repositories plan 03 may have deferred: without a sweep commit they print
# NOT SWEPT instead of failing. Plan 03 swept every repository (D-20
# complete), so the list is empty.
SWEEP_OPTIONAL=()
# The sweep commit's subject carries both strings.
SWEEP_SUBJECT=("console help" "(SummerCMS v")
# apps stage: "<label>|<path>".
APP_REPOS=(
"sm-demo-app|$DEMO_APP"
"sm-bm-app|$BM_APP"
"sm-grzybyfunkcjonalne-app|$GRZYBY_APP"
"sm-summercmsio-app|$SUMMERCMSIO_APP"
"fono""teka.go|$FONOTEKA"
)
# landing stage.
# TestTerminalCommands runs the landing page's commands against the deployed
# bootstrap script and needs network access, Docker and ports 54320 and
# 8080: it runs only with SUMMERCMS_TERMINAL_CHECK=1 and is NOT RUN otherwise.
LANDING_TERMINAL_TESTS=(TestTerminalRunScript TestLoadTerminal TestTerminalScript TestTerminalEnv)
ROADMAP_TESTS=(TestRoadmapParse TestRoadmapDeckOrder TestRoadmapNoClientNames TestRoadmapPolishTitles)
# docs stage: names of consuming applications (split so this file does not
# match itself), the decision note and the modules it must name.
APP_NAMES=("p[lł]yta""rium" "fono""teka" "wave""path" "horo""skopia" "bm\\.jakub""zych" "grzyby""funkcjonalne")
DECISION_NOTE=".planning/notes/2026-10-21-console-charm-stack.md"
DECISION_NAMES=(
"charm.land/lipgloss/v2" "github.com/charmbracelet/colorprofile" "charm.land/bubbletea/v2"
"charm.land/bubbles/v2" "charm.land/huh/v2" "charm.land/fang/v2"
)
NAME_SCOPE=(modules/diary modules/bonfire docs/console docs/services/logging.md)
usage() {
cat >&2 <<'EOF'
usage:
check-phase19.8.sh --self-test
check-phase19.8.sh go | hello | docs | plugins | apps | landing | docker
check-phase19.8.sh --go | --hello | --docs | --plugins | --apps | --landing | --docker
check-phase19.8.sh --all
EOF
exit 2
}
# detect_go reads go test -json. Exit 1 fail/build, 2 skip, 3 zero/no-tests,
# 4 non-JSON, 5 missing required name.
detect_go() {
python3 - "$1" <<'PY'
import json, os, sys
path = sys.argv[1]
require = [n for n in os.environ.get("REQUIRE_TESTS", "").split() if n]
passed = set()
failed = []
with open(path, encoding="utf-8", errors="replace") as fh:
for raw in fh:
line = raw.strip()
if not line.startswith("{"):
continue
try:
ev = json.loads(line)
except json.JSONDecodeError:
print("refuse: non-json test output", file=sys.stderr)
sys.exit(4)
action = ev.get("Action")
test = ev.get("Test") or ""
pkg = ev.get("Package") or ev.get("ImportPath") or ""
if action == "build-fail" or (action == "fail" and ev.get("FailedBuild")):
print(f"refuse: build failed {pkg}", file=sys.stderr)
sys.exit(1)
text = ev.get("Output") or ""
if action == "output" and "no tests to run" in text:
print(f"refuse: no tests to run in {pkg}", file=sys.stderr)
sys.exit(3)
if action == "skip" and test:
print(f"refuse: skipped {pkg} {test}", file=sys.stderr)
sys.exit(2)
if action == "fail":
failed.append(f"{pkg} {test}".strip())
if action == "pass" and test and "/" not in test:
passed.add(test)
if failed:
print("refuse: failed " + ", ".join(failed), file=sys.stderr)
sys.exit(1)
if not passed:
print("refuse: zero tests", file=sys.stderr)
sys.exit(3)
missing = [n for n in require if n not in passed]
if missing:
print("refuse: required tests did not pass: " + ", ".join(missing), file=sys.stderr)
sys.exit(5)
PY
}
# detect_tap reads node --test TAP output. Exit 1 a failed, skipped, todo
# or cancelled test (or a "not ok" line), 3 no passing summary.
detect_tap() {
python3 - "$1" <<'PY'
import re, sys
text = open(sys.argv[1], encoding="utf-8", errors="replace").read()
counts = {k: int(v) for k, v in re.findall(r"^# (tests|pass|fail|cancelled|skipped|todo) (\d+)\s*$", text, re.M)}
if re.search(r"^\s*not ok\b", text, re.M) or any(counts.get(k, 0) for k in ("fail", "cancelled", "skipped", "todo")):
print("refuse: TAP reports failed, cancelled, skipped or todo tests", file=sys.stderr)
sys.exit(1)
if counts.get("pass", 0) == 0 or counts.get("pass") != counts.get("tests"):
print("refuse: no all-passing TAP summary", file=sys.stderr)
sys.exit(3)
PY
}
# go_json DIR ARGS... runs go test -json ARGS in DIR and checks the stream
# with detect_go.
go_json() {
local dir="$1"
shift
local log err rc=0 dc=0
log="$(mktemp)"
err="$(mktemp)"
(cd "$dir" && go test -json "$@") >"$log" 2>"$err" || rc=$?
detect_go "$log" || dc=$?
if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then
cat "$err" >&2 || true
grep -v '^{' "$log" | tail -n 40 >&2 || true
python3 -c 'import json,sys
for raw in open(sys.argv[1], errors="replace"):
try: ev = json.loads(raw)
except ValueError: continue
if ev.get("Action") == "output" and ("FAIL" in ev.get("Output", "") or "SKIP" in ev.get("Output", "") or "_test.go:" in ev.get("Output", "")):
sys.stderr.write(ev["Output"])' "$log" 2>&1 | tail -n 40 >&2 || true
rm -f "$log" "$err"
echo "refuse: go test $* in $dir (test=$rc detect=$dc)" >&2
return 1
fi
echo "go: $(grep -c '"Action":"pass","Package":"[^"]*","Test":"[^"/]*","Elapsed"' "$log") top-level tests passed in $dir ($*)"
rm -f "$log" "$err"
}
# go_named DIR PACKAGES FLAGS NAME... runs exactly the named top-level tests
# and requires every one of them to pass. PACKAGES and FLAGS are word-split.
go_named() {
local dir="$1" pkgs="$2" flags="$3"
shift 3
if [[ "$#" -eq 0 ]]; then
echo "refuse: go_named $pkgs in $dir without required names" >&2
return 1
fi
local names=("$@") regex
regex="^($(IFS='|'; echo "${names[*]}"))\$"
# shellcheck disable=SC2086
REQUIRE_TESTS="${names[*]}" go_json "$dir" $flags -count=1 -run "$regex" $pkgs
}
# go_short DIR PACKAGES runs go test -short PACKAGES in DIR; on failure it
# prints the tail of the output and fails.
go_short() {
local dir="$1" pkgs="${2:-./...}" out
# shellcheck disable=SC2086
if ! out="$(cd "$dir" && go test -count=1 -short $pkgs 2>&1)"; then
printf '%s\n' "$out" | grep -v '^ok ' | tail -n 60 >&2
echo "refuse: go test -short $pkgs in $dir" >&2
return 1
fi
echo "short: $dir ($pkgs)"
}
go_vet() {
local dir="$1"
shift
(cd "$dir" && go vet "$@")
echo "vet: $dir ($*)"
}
require_dir() {
[[ -d "$1" ]] || {
echo "refuse: $2 $1 not found" >&2
return 1
}
}
expect_detector() {
local detector="$1" name="$2" want="$3" payload="$4" log dc=0
log="$(mktemp)"
printf '%s\n' "$payload" >"$log"
"$detector" "$log" >/dev/null 2>&1 || dc=$?
rm -f "$log"
if [[ "$dc" -ne "$want" ]]; then
echo "refuse: self-test $detector $name: exit $dc, want $want" >&2
return 1
fi
}
# expect_mode NAME WANT_EXIT WANT_TEXT MODE ENV... runs this script in MODE
# with ENV and checks its exit status and that its output carries WANT_TEXT.
expect_mode() {
local name="$1" want="$2" text="$3" mode="$4"
shift 4
local out rc=0
out="$(env "$@" bash "${BASH_SOURCE[0]}" "$mode" 2>&1)" || rc=$?
if [[ "$rc" -ne "$want" ]] || ! grep -qF -- "$text" <<<"$out"; then
printf '%s\n' "$out" | tail -n 10 >&2
echo "refuse: self-test $name: exit $rc (want $want), output must carry \"$text\"" >&2
return 1
fi
}
# removed_exports reads a unified diff and prints every removed line that
# declares an exported func, method or type.
removed_exports() {
grep -E '^-(func ([A-Z]|\([^)]*\) [A-Z])|type [A-Z])' | grep -vE '^--- ' || true
}
# removed_names reads a unified diff and prints every Name: "<x>" literal
# (a command, flag or argument name) on a removed line that no added line
# carries.
removed_names() {
python3 -c '
import re, sys
removed, added = set(), set()
for line in sys.stdin:
if line.startswith(("---", "+++")):
continue
names = re.findall(r"\bName:\s*\"([^\"]+)\"", line)
if line.startswith("-"):
removed.update(names)
elif line.startswith("+"):
added.update(names)
for n in sorted(removed - added):
print(n)
'
}
# sweep_commit DIR prints the hash of the newest commit whose subject
# carries every SWEEP_SUBJECT string, or nothing.
sweep_commit() {
local args=() s
for s in "${SWEEP_SUBJECT[@]}"; do
args+=(--grep="$s")
done
git -C "$1" log -F --all-match "${args[@]}" --format=%H -n 1 HEAD
}
# additive_check LABEL DIR checks the sweep commit of the repository at DIR:
# no exported declaration and no Name literal removed. Without a sweep
# commit it prints NOT SWEPT when LABEL is in SWEEP_OPTIONAL and fails
# otherwise.
additive_check() {
local label="$1" dir="$2" sweep diff rc=0 removed names optional
sweep="$(sweep_commit "$dir")" || {
echo "refuse: git log in $dir failed" >&2
return 1
}
if [[ -z "$sweep" ]]; then
for optional in "${SWEEP_OPTIONAL[@]}"; do
if [[ "$optional" == "$label" ]]; then
echo "NOT SWEPT $label (no sweep commit; plan 03 deferred it)"
return 0
fi
done
echo "refuse: $label has no sweep commit (subject with: ${SWEEP_SUBJECT[*]})" >&2
return 1
fi
diff="$(git -C "$dir" diff "$sweep^" "$sweep" -- '*.go' ':(exclude)*_test.go' 2>&1)" || rc=$?
if [[ "$rc" -ne 0 ]]; then
printf '%s\n' "$diff" | tail -n 5 >&2
echo "refuse: git diff of the sweep commit $sweep in $dir failed (exit $rc)" >&2
return 1
fi
removed="$(removed_exports <<<"$diff")"
if [[ -n "$removed" ]]; then
printf '%s\n' "$removed" >&2
echo "refuse: $label's sweep commit ${sweep:0:7} removed or changed exported declarations" >&2
return 1
fi
names="$(removed_names <<<"$diff")"
if [[ -n "$names" ]]; then
printf '%s\n' "$names" >&2
echo "refuse: $label's sweep commit ${sweep:0:7} removed command, flag or argument names" >&2
return 1
fi
echo "additive: $label sweep ${sweep:0:7} removes no export and no command, flag or argument name"
}
# planted_repo DIR builds a git repository at DIR with a base commit (an
# exported func, a command name and a flag name), then the commit SUBJECT
# whose file content is BODY.
planted_repo() {
local dir="$1" subject="$2" body="$3"
mkdir -p "$dir"
git -C "$dir" init -q
git -C "$dir" config user.email gate@example.test
git -C "$dir" config user.name gate
printf '%s\n' 'package p' '' 'func Exported() {}' '' 'var c = Command{Name: "acme:sync", Flags: []Flag{{Name: "dry-run"}}}' >"$dir/p.go"
git -C "$dir" add p.go
git -C "$dir" commit -q -m "base"
printf '%s\n' "$body" >"$dir/p.go"
git -C "$dir" add p.go
git -C "$dir" commit -q -m "$subject"
}
run_self_test() {
bash -n "${BASH_SOURCE[0]}"
expect_detector detect_go pass 0 '{"Action":"pass","Package":"p","Test":"TestDiaryTracerSmoke"}'
expect_detector detect_go fail 1 '{"Action":"pass","Package":"p","Test":"TestA"}
{"Action":"fail","Package":"p","Test":"TestHelpSmoke"}'
expect_detector detect_go package-fail 1 '{"Action":"pass","Package":"p","Test":"TestA"}
{"Action":"fail","Package":"p"}'
expect_detector detect_go build 1 '{"Action":"build-fail","ImportPath":"p"}'
expect_detector detect_go skip 2 '{"Action":"pass","Package":"p","Test":"TestA"}
{"Action":"skip","Package":"p","Test":"TestServeLifecycle"}'
expect_detector detect_go subtest-skip 2 '{"Action":"skip","Package":"p","Test":"TestPeekGlobals/env"}
{"Action":"pass","Package":"p","Test":"TestPeekGlobals"}'
expect_detector detect_go subtest-only-pass 3 '{"Action":"pass","Package":"p","Test":"TestOuter/TestListTracerSmoke"}
{"Action":"pass","Package":"p"}'
expect_detector detect_go zero 3 '{"Action":"pass","Package":"p"}'
expect_detector detect_go no-tests 3 '{"Action":"output","Package":"p","Output":"testing: warning: no tests to run\n"}
{"Action":"pass","Package":"p"}'
expect_detector detect_go nonjson 4 '{"Action":"pass",'
REQUIRE_TESTS="TestDiaryTracerSmoke TestConfigEnvSmoke" expect_detector detect_go missing-named 5 \
'{"Action":"pass","Package":"p","Test":"TestDiaryTracerSmoke"}'
REQUIRE_TESTS="TestHelp" expect_detector detect_go prefix-is-not-the-test 5 \
'{"Action":"pass","Package":"p","Test":"TestHelpSmoke"}'
REQUIRE_TESTS="TestListTracerSmoke" expect_detector detect_go subtest-is-not-the-test 5 \
'{"Action":"pass","Package":"p","Test":"TestOuter/TestListTracerSmoke"}
{"Action":"pass","Package":"p","Test":"TestOther"}'
local tap_ok tap_fail tap_skip tap_todo tap_none
tap_ok=$'ok 1 - a\nok 2 - b\n1..2\n# tests 2\n# suites 0\n# pass 2\n# fail 0\n# cancelled 0\n# skipped 0\n# todo 0'
tap_fail=$'ok 1 - a\nnot ok 2 - b\n1..2\n# tests 2\n# pass 1\n# fail 1\n# cancelled 0\n# skipped 0\n# todo 0'
tap_skip=$'ok 1 - a\nok 2 - b # SKIP\n1..2\n# tests 2\n# pass 1\n# fail 0\n# cancelled 0\n# skipped 1\n# todo 0'
tap_todo=$'ok 1 - a\nok 2 - b # TODO\n1..2\n# tests 2\n# pass 1\n# fail 0\n# cancelled 0\n# skipped 0\n# todo 1'
tap_none=$'1..0\n# tests 0\n# pass 0\n# fail 0\n# cancelled 0\n# skipped 0\n# todo 0'
expect_detector detect_tap pass 0 "$tap_ok"
expect_detector detect_tap fail 1 "$tap_fail"
expect_detector detect_tap skip 1 "$tap_skip"
expect_detector detect_tap todo 1 "$tap_todo"
expect_detector detect_tap zero 3 "$tap_none"
expect_detector detect_tap no-summary 3 'ok 1 - a'
# The NOT RUN rule, on a faked host: a docker binary that never answers.
local tmp
tmp="$(mktemp -d)"
mkdir -p "$tmp/bin"
printf '#!/bin/sh\nexit 1\n' >"$tmp/bin/docker"
chmod +x "$tmp/bin/docker"
expect_mode docker-not-run 0 "NOT RUN docker" --docker-gate PATH="$tmp/bin:$PATH" PHASE198_REQUIRE_DOCKER=0
expect_mode docker-required 1 "FAIL docker" --docker-gate PATH="$tmp/bin:$PATH" PHASE198_REQUIRE_DOCKER=1
expect_mode docker-stage-needs-daemon 1 "needs a reachable Docker daemon" --docker PATH="$tmp/bin:$PATH"
rm -rf "$tmp"
# The additive check on planted repositories: a sweep that adds is
# accepted; one that removes an export, a command name or a flag name is
# refused; a repository without a sweep commit prints NOT SWEPT only when
# it is optional.
local repos sweep_subject="feat: console help, examples and named loggers (SummerCMS v0.4.0)"
repos="$(mktemp -d)"
planted_repo "$repos/ok" "$sweep_subject" "$(printf '%s\n' 'package p' '' 'func Exported() {}' 'func Added() {}' '' 'var c = Command{' ' Name: "acme:sync",' ' Flags: []Flag{{Name: "dry-run"}, {Name: "json", Bare: true}},' '}')"
planted_repo "$repos/export" "$sweep_subject" "$(printf '%s\n' 'package p' '' 'func exported() {}' '' 'var c = Command{Name: "acme:sync", Flags: []Flag{{Name: "dry-run"}}}')"
planted_repo "$repos/command" "$sweep_subject" "$(printf '%s\n' 'package p' '' 'func Exported() {}' '' 'var c = Command{Name: "acme:synchronise", Flags: []Flag{{Name: "dry-run"}}}')"
planted_repo "$repos/flag" "$sweep_subject" "$(printf '%s\n' 'package p' '' 'func Exported() {}' '' 'var c = Command{Name: "acme:sync", Flags: []Flag{{Name: "pretend"}}}')"
planted_repo "$repos/unswept" "feat: something else" "$(printf '%s\n' 'package p' '' 'func Exported() {}')"
local case_name want rc
for case_name in ok:0 export:1 command:1 flag:1 unswept:1; do
want="${case_name#*:}"
case_name="${case_name%%:*}"
rc=0
additive_check "$case_name" "$repos/$case_name" >/dev/null 2>&1 || rc=$?
if [[ "$rc" -ne "$want" ]]; then
rm -rf "$repos"
echo "refuse: self-test additive_check $case_name: exit $rc, want $want" >&2
return 1
fi
done
local out
out="$(SWEEP_OPTIONAL=(unswept); additive_check unswept "$repos/unswept" 2>&1)" || {
rm -rf "$repos"
echo "refuse: self-test additive_check refused an optional unswept repository" >&2
return 1
}
if [[ "$out" != "NOT SWEPT unswept"* ]]; then
rm -rf "$repos"
echo "refuse: self-test additive_check did not print NOT SWEPT: $out" >&2
return 1
fi
rm -rf "$repos"
local mode
for mode in self-test go hello docs plugins apps landing docker all; do
grep -q -- "^ --$mode" "${BASH_SOURCE[0]}" || {
echo "refuse: missing mode --$mode" >&2
return 1
}
done
echo "phase19.8 self-test passed"
}
run_go() {
go_vet "$ROOT" ./...
go_short "$ROOT"
go_named "$ROOT" ./modules/diary -short "${DIARY_TESTS[@]}"
go_named "$ROOT" ./modules/sunscreen -short "${SUNSCREEN_TESTS[@]}"
go_named "$ROOT" ./modules/bonfire -short "${BONFIRE_TESTS[@]}"
go_named "$ROOT" ./internal/docsite -short "${DOCSITE_TESTS[@]}"
go_named "$ROOT" ./modules/conga -short "${CONGA_TESTS[@]}"
go_named "$ROOT" ./modules/lagoon -short "${LAGOON_TESTS[@]}"
go_named "$ROOT" ./modules/surf -short "${SURF_TESTS[@]}"
go_named "$ROOT" ./modules/flare -short "${FLARE_TESTS[@]}"
go_named "$ROOT" ./internal/dev -short "${DEV_TESTS[@]}"
go_named "$ROOT" ./cmd/summer -short "${SUMMER_TESTS[@]}"
local out
if ! out="$(cd "$ROOT" && go test -count=1 -race -short ./modules/diary/... ./modules/sunscreen/... ./modules/bonfire/... ./internal/docsite/... 2>&1)"; then
printf '%s\n' "$out" | tail -n 60 >&2
echo "refuse: go test -race -short of diary, sunscreen, bonfire and docsite" >&2
return 1
fi
echo "race: modules/diary, modules/sunscreen, modules/bonfire, internal/docsite"
echo "phase19.8 go passed"
}
run_hello() {
local hello="$ROOT/examples/hello"
require_dir "$hello" "example application"
go_vet "$hello" ./...
go_json "$hello" -count=1 ./...
go_named "$hello" . "" "${HELLO_TESTS[@]}"
echo "phase19.8 hello passed"
}
run_docs() {
local name
for name in "${DECISION_NAMES[@]}"; do
if ! grep -qF -- "$name" "$ROOT/$DECISION_NOTE"; then
echo "refuse: the decision note $DECISION_NOTE does not name $name" >&2
return 1
fi
done
echo "note: $DECISION_NOTE names the six charm modules"
go_named "$ROOT" ./cmd/summer "" TestDocsTree
(cd "$ROOT" && go run ./cmd/summer docs:build --check)
local pattern hits files=() scope
pattern="$(IFS='|'; echo "${APP_NAMES[*]}")"
for scope in "${NAME_SCOPE[@]}"; do
[[ -e "$ROOT/$scope" ]] || {
echo "refuse: $scope not found" >&2
return 1
}
while IFS= read -r -d '' f; do
files+=("$f")
done < <(find "$ROOT/$scope" -type f -print0)
done
hits="$(grep -inHIE "$pattern" "${files[@]}" || true)"
if [[ -n "$hits" ]]; then
printf '%s\n' "$hits" | head -n 20 >&2
echo "refuse: a console or logging source or page names a consuming application" >&2
return 1
fi
echo "names: ${#files[@]} files under ${NAME_SCOPE[*]} checked, no consuming application named"
local listing rc=0
listing="$(cd "$ROOT" && go run ./cmd/summer list --format=json)" || rc=$?
if [[ "$rc" -ne 0 ]]; then
echo "refuse: summer list --format=json failed (exit $rc)" >&2
return 1
fi
python3 -c '
import json, sys
listing = json.load(sys.stdin)
commands = listing.get("commands") or []
missing = [c["name"] for c in commands if not c.get("examples")]
if not commands or missing:
print("refuse: summer commands without examples: " + ", ".join(missing or ["(no commands)"]), file=sys.stderr)
sys.exit(1)
print(f"examples: every one of the {len(commands)} summer commands has examples")
' <<<"$listing"
echo "phase19.8 docs passed"
}
run_plugins() {
local entry label dir
for entry in "${PLUGIN_REPOS[@]}"; do
label="${entry%%|*}"
dir="${entry#*|}"
require_dir "$dir" "$label"
echo "== $label"
if ! {
go_vet "$dir" ./... &&
go_short "$dir" &&
go_named "$dir" . -short TestCommandsDocumented &&
(cd "$dir" && GOWORK=off go build ./...) &&
echo "build: GOWORK=off go build ./... in $dir" &&
additive_check "$label" "$dir"
}; then
echo "refuse: plugin $label" >&2
return 1
fi
done
echo "phase19.8 plugins passed (${#PLUGIN_REPOS[@]} repositories)"
}
run_apps() {
local tmp summer entry label dir binary dirty out
tmp="$(mktemp -d)"
summer="$tmp/summer"
(cd "$ROOT" && go build -o "$summer" ./cmd/summer)
for entry in "${APP_REPOS[@]}"; do
label="${entry%%|*}"
dir="${entry#*|}"
require_dir "$dir" "$label" || {
rm -rf "$tmp"
return 1
}
echo "== $label"
binary="$(sed -nE 's/^binary:[[:space:]]*"?([^"[:space:]]+)"?.*/\1/p' "$dir/summer.yaml" | head -n 1)"
if [[ -z "$binary" ]]; then
rm -rf "$tmp"
echo "refuse: $label/summer.yaml names no binary" >&2
return 1
fi
dirty="$(git -C "$dir" status --porcelain -- main.go plugins.gen.go)"
if [[ -n "$dirty" ]]; then
printf '%s\n' "$dirty" >&2
rm -rf "$tmp"
echo "refuse: $label has uncommitted generated files before the build" >&2
return 1
fi
mkdir -p "$tmp/$label"
if ! (cd "$dir" && go build -o "$tmp/$label/" ./...); then
rm -rf "$tmp"
echo "refuse: go build ./... in $label" >&2
return 1
fi
echo "build: go build ./... in $dir"
if ! out="$(cd "$dir" && "$summer" build 2>&1)"; then
printf '%s\n' "$out" | tail -n 30 >&2
rm -rf "$tmp"
echo "refuse: summer build in $label" >&2
return 1
fi
if ! git -C "$dir" diff --exit-code --stat -- main.go plugins.gen.go >&2; then
rm -rf "$tmp"
echo "refuse: summer build changed $label's committed main.go or plugins.gen.go (report it; plan 03 committed the regenerated main)" >&2
return 1
fi
echo "generated: summer build leaves $label's main.go and plugins.gen.go unchanged"
# The user plugin needs a JWT secret to boot; a throwaway value when
# the application does not set one.
if ! out="$(cd "$dir" && SUMMER_GOLEM15__USER__JWT__SECRET="${SUMMER_GOLEM15__USER__JWT__SECRET:-gate-only-throwaway-value-0123456789abcdef}" "./bin/$binary" list 2>&1)" ||
! grep -q '^Available commands:' <<<"$out"; then
printf '%s\n' "$out" | tail -n 30 >&2
rm -rf "$tmp"
echo "refuse: bin/$binary list in $label" >&2
return 1
fi
if ! out="$(cd "$dir" && SUMMER_GOLEM15__USER__JWT__SECRET="${SUMMER_GOLEM15__USER__JWT__SECRET:-gate-only-throwaway-value-0123456789abcdef}" "./bin/$binary" help migrate 2>&1)" ||
! grep -q '^Usage:' <<<"$out"; then
printf '%s\n' "$out" | tail -n 30 >&2
rm -rf "$tmp"
echo "refuse: bin/$binary help migrate in $label" >&2
return 1
fi
echo "boot: bin/$binary list and help migrate exit 0"
done
rm -rf "$tmp"
echo "phase19.8 apps passed (${#APP_REPOS[@]} applications)"
}
run_landing() {
local vue="$SUMMERCMSIO_APP/vue-summercmsio-app" log rc=0 dc=0
require_dir "$vue" "landing site"
require_dir "$DEMO_APP" "demo application"
log="$(mktemp)"
if ! npm --prefix "$vue" run generate >"$log" 2>&1; then
tail -n 30 "$log" >&2
rm -f "$log"
echo "refuse: npm run generate in $vue" >&2
return 1
fi
echo "generate: $vue/.output/public"
npm --prefix "$vue" run test >"$log" 2>&1 || rc=$?
detect_tap "$log" || dc=$?
if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then
grep -E '^\s*not ok|^# ' "$log" | tail -n 30 >&2
rm -f "$log"
echo "refuse: npm run test in $vue (test=$rc detect=$dc)" >&2
return 1
fi
echo "tap: $(grep -E '^# (tests|pass) ' "$log" | tr '\n' ' ')"
rm -f "$log"
go_named "$SUMMERCMSIO_APP" . -short "${LANDING_TERMINAL_TESTS[@]}"
if [[ "${SUMMERCMS_TERMINAL_CHECK:-}" == "1" ]]; then
go_named "$SUMMERCMSIO_APP" . "-timeout 15m" TestTerminalCommands
else
echo "NOT RUN TestTerminalCommands (the deployed one-liner end to end: needs network, Docker and ports 54320 and 8080; set SUMMERCMS_TERMINAL_CHECK=1)"
fi
go_named "$DEMO_APP" ./plugins/demo/roadmap -short "${ROADMAP_TESTS[@]}"
local ref="${ROADMAP_REF:-origin/master}"
if ! ROADMAP_REF="$ref" "$DEMO_APP/scripts/sync-roadmap.sh" --check; then
echo "refuse: the demo roadmap snapshot differs from $ref" >&2
return 1
fi
echo "roadmap: snapshot matches $ref"
echo "phase19.8 landing passed"
}
docker_up() {
command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1
}
run_docker() {
if ! docker_up; then
echo "refuse: the docker stage needs a reachable Docker daemon (testcontainers); run it on a Docker host" >&2
return 1
fi
go_named "$ROOT" ./modules/surf "-timeout 10m" "${DOCKER_SURF_TESTS[@]}"
go_named "$ROOT" ./modules/lagoon "-timeout 10m" "${DOCKER_LAGOON_TESTS[@]}"
go_named "$ROOT" ./modules/cabana "-timeout 10m" "${DOCKER_CABANA_TESTS[@]}"
echo "phase19.8 docker passed"
}
# docker_gate is the docker step of --all: run the stage when a daemon
# answers, fail with PHASE198_REQUIRE_DOCKER=1, print NOT RUN otherwise.
docker_gate() {
if docker_up; then
if bash "${BASH_SOURCE[0]}" --docker; then
echo "PASS docker"
return 0
fi
echo "FAIL docker"
return 1
fi
if [[ "${PHASE198_REQUIRE_DOCKER:-0}" == "1" ]]; then
echo "FAIL docker: no reachable Docker daemon and PHASE198_REQUIRE_DOCKER=1"
return 1
fi
cat >&2 <<'EOF'
************************************************************************
NOT RUN docker: no reachable Docker daemon on this host.
The testcontainers suites (Postgres) of the framework did NOT run. Run on
a Docker host (through sudo -u golem when the socket is denied) before
/msd-verify-work:
bash scripts/check-phase19.8.sh docker
************************************************************************
EOF
echo "NOT RUN docker (Docker-host only; see the notice above)"
}
run_all() {
local stage
for stage in self-test go hello docs plugins apps landing; do
if bash "${BASH_SOURCE[0]}" "--$stage"; then
echo "PASS $stage"
else
echo "FAIL $stage"
exit 1
fi
done
docker_gate || exit 1
echo "Phase 19.8 gate passed"
}
case "${1:---all}" in
--self-test) run_self_test ;;
--go) run_go ;;
--hello) run_hello ;;
--docs) run_docs ;;
--plugins) run_plugins ;;
--apps) run_apps ;;
--landing) run_landing ;;
--docker) run_docker ;;
--docker-gate) docker_gate ;;
--all) run_all ;;
self-test) run_self_test ;;
go) run_go ;;
hello) run_hello ;;
docs) run_docs ;;
plugins) run_plugins ;;
apps) run_apps ;;
landing) run_landing ;;
docker) run_docker ;;
all) run_all ;;
*) usage ;;
esac