Compare commits

..

75 Commits

Author SHA1 Message Date
Jakub Zych
ee5b8844bf docs(todo): user logout bug confirmed, fixed in the plugin and the demo frontend 2026-10-22 04:57:09 +02:00
Jakub Zych
1e9927fa4b docs(19.8): map phase and its todos to g15office tasks 2026-10-22 04:41:51 +02:00
Jakub Zych
6267edb7c5 test(19.8): persist human verification items as UAT 2026-10-22 04:41:10 +02:00
Jakub Zych
eaeb2347c0 docs(19.8): re-verification, gaps closed (57/57) 2026-10-22 04:41:10 +02:00
Jakub Zych
4141cd916f docs(quick-261022-4z8): Fix the open 19.8 re-review findings (WR-09..WR-14, IN-14, IN-15) 2026-10-22 04:31:34 +02:00
Jakub Zych
b1fba7e24c docs(19.8): WR-12 disposition names the Status rework (139f5e3) 2026-10-22 04:30:41 +02:00
Jakub Zych
139f5e3942 fix(lagoon): keep Status plugin-only; migrate:status adds the framework sets itself (WR-12)
57a849d made lagoon.Status read migrationSets, which changed its result
for consumers: the framework sets came first and a plugin with no
migrations lost its row. Status is back to its pre-57a849d behaviour.
migrate:status (table and --json) now reads the unexported setStatus,
which lists the framework sets and the plugins from migrationSets, so it
still agrees with migrate --pretend. Both share historyRows.

- TestStatusPluginRows pins Status: plugin rows only, an empty row for a
  plugin with no migrations or no history table, none without HasMigrations
- TestMigrateStatusJSON also checks the table lists the framework sets
- TestTwoPluginMigrationSetsIsolated and TestDocsMigrate are back to the
  plugin-only rows
2026-10-22 04:30:36 +02:00
Jakub Zych
f5e9ee508b docs(todo): user plugin logout leaves the session cookie valid 2026-10-22 04:21:27 +02:00
Jakub Zych
a48afbd11e docs(19.8): re-review fixes recorded, IN-10..IN-13 and the exit-130 suggestion deferred 2026-10-22 04:10:42 +02:00
Jakub Zych
0691d26f1f fix(lagoon): warn instead of failing when the re-read after a successful migrate fails (WR-13) 2026-10-22 04:04:32 +02:00
Jakub Zych
57a849d114 fix(lagoon): migrate:status reads the sets migrate applies (WR-12) 2026-10-22 04:03:19 +02:00
Jakub Zych
17c722da46 fix(summer): mask secret flag values in a delegate's error (WR-10) 2026-10-22 04:00:57 +02:00
Jakub Zych
707372bf65 fix(bonfire): mask secret flag values that start with a dash (WR-11) 2026-10-22 03:57:55 +02:00
Jakub Zych
fce232d7c3 fix(bonfire): end a live view when its context is cancelled and print later lines plainly (WR-09) 2026-10-22 03:56:37 +02:00
Jakub Zych
1ac75ad59a fix(bonfire): claim the live view slot before building the program (IN-15) 2026-10-22 03:53:29 +02:00
Jakub Zych
48e13f9b57 fix(bonfire): strip control characters from Process's plain item lines and the view's label (WR-14) 2026-10-22 03:52:37 +02:00
Jakub Zych
7c1509581b docs(bonfire): the ErrReported comment names the WithExitCode code (IN-14) 2026-10-22 03:51:55 +02:00
Jakub Zych
a28957f7d0 docs(quick-261022-2zy): plan and summary for the user and sociallogin plugin security fixes 2026-10-22 03:38:47 +02:00
Jakub Zych
36e2240891 docs(19.8): phase verification, three gaps from the re-review 2026-10-22 03:34:48 +02:00
Jakub Zych
9516a2b36f feat(cabana): ClearOnChange clears dependent columns when a relation changes a foreign key
RelationContract (hasMany) and FieldRelationContract (belongsTo) accept
ClearOnChange, a list of nullable columns set to NULL whenever the relation
changes the foreign key: a hasMany link and unlink (deferred ones included)
clear them on the child, and a form save that moves a belongsTo key to
another record or to none clears them on the record unless the same save
writes the column through its own field. Boot refuses the option on a
belongsToMany and for a missing, non-nullable, foreign-key or primary-key
column. The zero value changes nothing.
2026-10-22 03:30:35 +02:00
Jakub Zych
9563bc836a docs(19.8): record code review disposition 2026-10-22 03:19:33 +02:00
Jakub Zych
28ad561481 docs(19.8): re-review of the code review fixes 2026-10-22 03:19:15 +02:00
Jakub Zych
0ebed687a0 docs(19.8-06): roadmap progress 6/6 plans 2026-10-22 03:07:14 +02:00
Jakub Zych
349e2f810b docs(19.8-06): v0.4.2 released, demo roadmap synced, phase gate green 2026-10-22 03:07:14 +02:00
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
117 changed files with 20066 additions and 285 deletions

View File

@@ -46,5 +46,7 @@ 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 |
| | | 19.8 | t9k2efpy |
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:** 6/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
- [x] 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) | 6/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,81 @@
---
status: awaiting_human_verify
trigger: "User plugin logout: auth cookies not invalidated at once; requests keep authenticating ~60 min until token expiry (todo 2026-10-22-user-logout-cookie-stays-valid)"
created: 2026-10-22T00:00:00Z
updated: 2026-10-22T00:00:00Z
---
## Current Focus
bug_class: Bohrbug (deterministic)
reasoning_checkpoint:
hypothesis: "Logout reads only the bearer header (cookie sessions get 401, nothing revoked, cookie kept, jwt guard accepts the cookie until exp = 60 min), never writes a forget cookie, and blacklists the jti only until the access exp (so /refresh revives a logged-out token after exp + sweep)"
confirming_evidence:
- "probe: cookie-only logout 401, then GET /tokens with the cookie 200"
- "probe: jwt_blacklist.expires_at == exp; after the sweep, /refresh of the logged-out token 200"
- "bearer logout already refuses the bearer at once (guard checks blacklist) - so server-side check exists; the gap is what logout revokes and for how long"
falsification_test: "if cookie-only logout after the fix still leaves GET /tokens 200, or /refresh after the sweep still 200, hypothesis/fix wrong"
fix_rationale: "read the token exactly as the jwt guard does, revoke it (and any other session token the request carries) until the refresh window closes, accept an expired-but-refreshable token like cabana does, and expire the session cookies with auth.cookie attributes"
blind_spots: "demo frontend never calls /logout (app-side, not fixable here); auth_token cookie is frontend-owned, its real attributes unknown (forget uses auth.cookie path/domain)"
candidate_causes:
- "code: Logout bearer-only extraction + exp-only blacklist expiry"
- "config: blacklist store not published (no DB) -> no revocation; ruled out, Boot publishes PostgresBlacklist when *sql.DB/*gorm.DB exist and bearer logout works"
- "environment/frontend: demo frontend logout only clears its own cookie and never calls /logout"
and_gate: "no for the cookie case (extraction alone suffices); the refresh revival is an independent second defect"
next_action: human verification in a real app (cookie-only logout, then a jwt.auth request with the old cookie must be 401); after confirmation archive to resolved/ and add the KB entry
## Symptoms
expected: after POST /_user/api/v1/logout the old token/cookie is refused (401) on the next request, and the cookies are cleared
actual: requests keep authenticating for about 60 minutes, until the token expires
errors: none
reproduction: login, logout, repeat an authenticated request with the old cookie or token
started: reported 2026-10-22
## Eliminated
## Evidence
- timestamp: 2026-10-22
checked: bouncer jwtGuard.Authenticate (summercms.go modules/bouncer/jwt.go)
found: access JWTs are checked for HS256 signature, exp, audience, the subject user (GormUsers.FindByID), the jti blacklist (BlacklistStore) and users.tokens_valid_after; token taken from bearer first, then cookies "token", "auth_token" (plugin.go Boot)
implication: server-side revocation exists (jwt_blacklist); a blacklisted jti is refused on the next request
- timestamp: 2026-10-22
checked: controllers.Logout (api_controller.go)
found: reads bearerToken(r) only (Authorization header); no cookie fallback; never writes a Set-Cookie; blacklists with Add(jti, exp, now) i.e. storage expiry = access exp
implication: cookie-only logout => 401, nothing revoked, cookie kept; blacklist row is dropped after exp although the refresh window (20160 min) is still open
- timestamp: 2026-10-22
checked: sociallogin writeSession (token_cookie: true) and bouncer.CookieConfig.TokenCookie
found: sets httpOnly `token` cookie (auth.cookie.* name/path/domain/samesite, Max-Age = refresh_ttl) and leaves token out of the body, so the browser cannot send a bearer header
implication: such a frontend can only log out with the cookie => H1 path
- timestamp: 2026-10-22
checked: bouncer.refreshAudience comment and blacklist storage expiry
found: Refresh stores the old jti until max(exp, iat+refreshTTL)+1min precisely so "a logged-out token cannot be refreshed again"; Logout does not
implication: H2 plausible
- timestamp: 2026-10-22
checked: PHP ApiController::logout + AuthManager::logout(true)
found: token from Bearer or jwt_token param; JWTAuth::invalidate(forceForever=true) => blacklisted forever; body {"message":"Logged out"}
implication: Go logout should keep the jti blocked at least for the refresh window
- timestamp: 2026-10-22
checked: cabana admin logout (summercms.go modules/cabana/auth.go logout)
found: framework reference: always expires the cookie, reads bearer then cookie, VerifyRefreshableClaimsAudience, blacklists until max(exp, iat+refreshTTL)+1min
implication: the plugin's logout lags the framework's own pattern
- timestamp: 2026-10-22
checked: Docker probe through the assembled router (newAdminEnv), pre-fix
found: (A) cookie-only POST /logout -> 401 {"error":true,"message":"Unauthorized"}, no Set-Cookie; GET /tokens (jwt.auth) with the same cookie afterwards -> 200; token exp-iat = 1h0m0s. (B) bearer logout -> 200 and the bearer is refused at once (401), but jwt_blacklist.expires_at == token exp; the sweep after exp deletes the row and POST /refresh with the logged-out token -> 200 with a fresh token
implication: H1 and H2 CONFIRMED. New test logout_test.go fails 7/8 subtests pre-fix (bearer passes)
## Resolution
root_cause: "controllers.Logout read the token only from the Authorization bearer header, although the jwt guard (and RequestUser) also accept the token/auth_token cookies: a cookie-carried session (e.g. the httpOnly token cookie sociallogin token_cookie sets) got 401 at /logout, nothing was blacklisted and no cookie was cleared, so the cookie kept authenticating jwt.auth routes until exp (golem15.user.jwt.ttl, 60 min). Logout also never wrote a forget cookie; and it (and EndRequestSession) kept the jwt_blacklist row only until the access exp, so after the sweep /refresh revived a logged-out token for the rest of the 20160-min refresh window."
fix: "classes.EndSession (new export): revokes every session JWT the request carries (bearer, token, auth_token) that verifies - unexpired, or user-audience within its refresh window - until max(exp, iat+refresh_ttl)+1m; always writes the auth.cookie forget cookie plus forgets for other carried session cookies with the same attributes; returns the guard-order subject. Logout uses it (responses unchanged: 200 Logged out / 401 Unauthorized / 500); EndRequestSession is now its best-effort wrapper."
verification:
target_test: { result: pass, test: "logout_test.go TestLogoutEndsTheSession (10 subtests); TestEndRequestSession updated" }
mutation_check: { result: pass, reason_if_skipped: "no Stryker for Go - manual mutants", mutant_killed: "7/7 (expiry-at-exp, bearer-only, no-forget, no-refreshable-verify, primary-only revoke, config-cookie-only forget, no legacy fallback)" }
no_op_deletion: { result: pass, deletion_justified_by_rca: true }
adjacent_tests: { result: pass, suites_run: ["sm-user-plugin go test ./... (Docker, all ok)", "sm-demo-app workspace: oauthserver + sociallogin go test (only TestClientRegistry/verify_secret and TestPHPMethodMap fail: they read ../summercms.go and ../fonoteka.go paths that exist only in the plugins' own repo layout; unrelated to the change)"] }
revert_and_reconfirm: { result: pass, bug_returned_on_revert: true, fixed_on_reapply: true }
guardrail_verdict: accepted
commit: 358ee5d (sm-user-plugin, unpushed)
files_changed: [classes/session_user.go, controllers/api_controller.go, session_user_test.go, logout_test.go, README.md]

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,229 @@
---
phase: 19.8-console-commands-overhaul
plan: 06
subsystem: release
tags: [release, v0.4.2, changelog, demo-roadmap, gate]
status: complete
gap_closure: true
requires:
- phase: 19.8-05
provides: the eleven review fixes and the Release impact table
provides:
- "SummerCMS v0.4.2: annotated tag 60b13ae on ead8e42, on origin with master fast-forwarded to it"
- "Changelog post https://summercms.io/changelog/summercms-0-4-2 (id 37)"
- "sm-demo-app roadmap snapshot synced to origin/master ead8e42 (aa4de22, pushed)"
- "Phase 19.8 --all gate green with Docker required"
affects: [applications and plugins pinning SummerCMS (fixes available from v0.4.2), demo roadmap deck]
tech-stack:
added: []
patterns: []
key-files:
created: []
modified:
- .planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md
- ../sm-demo-app/plugins/demo/content/ROADMAP.md
key-decisions:
- "Reply 'release' read as the proposed tag v0.4.2"
- "Tagged the commit that records the go-ahead (ead8e42, planning-only on top of the proposal) after vet, short tests and the docs checker passed there"
- "No plugin require bumps or application regenerate commits: no row of plan 05's Release impact table asks for one"
- "No Polish name needed: the roadmap change adds no phase (19.6.1 already had one, 762bb90)"
requirements-completed: [D-26]
actuals:
tokens: 4000 # chars/4 over the realized diff (this SUMMARY and the 6-line roadmap snapshot change)
tasks: 3
commits: 2 # MEASURED: git rev-list --count plan_head_before..HEAD before the final docs commit
plan_head_before: 8020b177ffc70f1b980ae769b80f55598998ce77
plan_head_after: ead8e422113d43c9ee828a92a628783634f65294
duration: 35min
completed: 2026-10-22
---
# Phase 19.8 Plan 06: Release of the Review Fixes Summary
**SummerCMS v0.4.2 is tagged and on origin, with its changelog post at https://summercms.io/changelog/summercms-0-4-2. The tag is annotated, on ead8e42, and master was fast-forwarded to exactly that commit. The demo roadmap snapshot follows the published roadmap, and the phase 19.8 `--all` gate is green with Docker required. Nothing was deployed or force-pushed.**
## 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.
The reply was recorded in `ead8e42 docs(19.8-06): record the go-ahead: release v0.4.2` before anything was tagged or pushed. That commit is the one tagged.
## Task 2: published
### Range check after a fresh fetch
- `git fetch origin`: exit 0, no output. origin/master was still `6c2164a`, so no other session had pushed.
- Newest tag: still **v0.4.1**, and origin had only v0.4.0 and v0.4.1 in `v0.4.*`. The proposed tag was still correct.
- `git log --format='%h %s' origin/master..ead8e42`: **20 commits**. These are the 18 classified in the proposal, plus `cf06185` (the proposal, which touches only `19.8-06-SUMMARY.md`) and `ead8e42` (the reply, the same file). Class totals: 12 plan 05 code commits, 8 planning documents of this phase, **0 planning documents of other sessions, 0 other code**.
- `git merge-base --is-ancestor origin/master ead8e42`: the push is a fast-forward.
### Checks at the tested commit `ead8e42`
GOCACHE pointed at the session scratchpad (tmpfs) for every go command. `go clean` was not run.
| Check | Result |
|-------|--------|
| `go vet ./...` | PASS |
| `go test -count=1 -short ./...` | PASS (exit 0, every package ok) |
| `go run ./cmd/summer docs:build --check` | PASS ("docs:build: no problems found") |
### Tag
- `git tag -a v0.4.2 ead8e42`: tag object **`60b13aee5dca32b7202e35f92e605652f66b8a97`**, `git cat-file -t v0.4.2` = `tag`, `v0.4.2^{commit}` = `ead8e422113d43c9ee828a92a628783634f65294`.
- Message: `SummerCMS v0.4.2`, then "Fixes from the review of the 0.4 console.", then a Fixed list of nine bullets in user words and an Added list (`bonfire.WithExitCode`, `bonfire.Sanitize`). It has no finding IDs and no planning or quick codes.
### Pushed ranges
| Repository | Push | Range |
|------------|------|-------|
| summercms (framework) | `git push origin ead8e42…:refs/heads/master` | `6c2164a..ead8e42` (fast-forward, 20 commits) |
| summercms (framework) | `git push origin v0.4.2` | new tag v0.4.2. `git ls-remote --exit-code --tags origin v0.4.2` → `60b13ae… refs/tags/v0.4.2` |
| sm-demo-app | `git push origin HEAD:refs/heads/master` | `762bb90..aa4de22` (1 commit, see Task 3) |
The Task 2 verify command (`ls-remote` the tag, fetch, the tagged commit is an ancestor of origin/master, the tag is annotated) passed.
### Changelog
- `scripts/changelog-post.py source v0.4.2` (from ../sm-summercmsio-app): TAG v0.4.2, DATE 2026-10-22T02:54:37+02:00, PREVIOUS v0.4.1, and 21 commit subjects.
- The notes file is in the session scratchpad (`v0.4.2-notes.md`). It has a one-line intro, `## Fixed` (nine bullets) and `## Added` (the two new functions). It has no H1, no planning IDs and no quick codes.
- `scripts/changelog-post.py publish v0.4.2 <notes> --excerpt "Console fixes for 0.4: ordered output under spinners, terminal restored on Ctrl+C, exit codes passed through by summer."` answered `posted id=37 https://summercms.io/changelog/summercms-0-4-2 (published_at 2026-10-22T00:54:37+00:00)`.
- **Post URL: https://summercms.io/changelog/summercms-0-4-2** (HTTP 200 right after publishing). The script read the token file itself. The token was never printed, copied or minted.
### Plugins and applications
Plugins and applications: nothing to publish. No row of plan 05's Release impact table asks for a require bump or a regenerate. The `--all` gate's plugins and apps stages confirm that every shared plugin and application still builds against the local framework, and that `summer build` leaves the generated main.go and plugins.gen.go unchanged.
## Task 3: demo roadmap and the gate
### Roadmap snapshot
- After the push, `ROADMAP_REF=origin/master ../sm-demo-app/scripts/sync-roadmap.sh --check` failed as expected: the snapshot differed from origin/master. This is 19.8's own plan list and progress.
- sm-demo-app before the change: `git status --porcelain` was empty and `origin/master..master` was empty after a fetch. Its HEAD was `762bb90 feat(demo): Polish name for roadmap phase 19.6.1`, already pushed by another session.
- `ROADMAP_REF=origin/master scripts/sync-roadmap.sh` wrote the snapshot from origin/master (ead8e42): `plugins/demo/content/ROADMAP.md`, 4 insertions and 2 deletions.
- TestRoadmapParse, TestRoadmapDeckOrder, TestRoadmapNoClientNames and TestRoadmapPolishTitles: PASS. No new phase, so **no Polish-name commit was needed**.
- Commit **`aa4de22 chore: sync the ROADMAP.md snapshot (summercms ead8e42)`**. It touches only `plugins/demo/content/ROADMAP.md` and was the only unpushed commit. It was pushed `762bb90..aa4de22`, and `origin/master..master` is now empty. Nothing was deployed.
### `--all` gate
`PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh --all` ran with GOCACHE and TMPDIR in the session scratchpad and Docker reached directly (no `sudo -u golem`). Exit 0. The landing pre-check `sync-roadmap --check` printed ok.
| Stage | Result | Notes |
|-------|--------|-------|
| self-test | PASS | |
| go | PASS | vet, short and the named tests: diary 30, sunscreen 2, bonfire 42, docsite 2, conga 5, lagoon 7, surf 13, flare 1, dev 4, cmd/summer 6. Race stage over diary, sunscreen, bonfire and docsite |
| hello | PASS | vet, 13 tests, then 5 named tests |
| docs | PASS | TestDocsTree; docs:build reported no problems; 67 files contain no client name; all 35 summer commands have examples |
| plugins | PASS | 15 shared plugins |
| apps | PASS | 5 applications (sm-demo-app, sm-bm-app, sm-grzybyfunkcjonalne-app, sm-summercmsio-app, fonoteka.go): build, generated files unchanged, `list` and `help migrate` exit 0 |
| landing | PASS | nuxt generate, tap 38/38, 4 terminal tests. **NOT RUN TestTerminalCommands** (allowed: needs network, Docker and ports; `SUMMERCMS_TERMINAL_CHECK=1`). 4 roadmap tests; "roadmap: snapshot matches origin/master" |
| docker | PASS | TestServeLifecycle; 6 lagoon tests including TestMigrateAppliedLines; 3 cabana tests |
Final line: **`Phase 19.8 gate passed`**. No line starts with `FAIL` or `NOT RUN docker`. The full log is `check-phase19.8-06.log` in the session scratchpad.
### Suggested g15office comment (for the orchestrator)
> Phase 19.8 review fixes released as SummerCMS v0.4.2 (changelog: https://summercms.io/changelog/summercms-0-4-2); phase gate green; ready for /msd-verify-work
## Deviations from Plan
**1. [Rule 3 - Blocking] Build cache on tmpfs.** `/home` is at 99% (5.1G free) and `/media/nvme` is at 99%. Every go command, including the gate, ran with `GOCACHE` (and, for the gate, `TMPDIR`) in the session scratchpad on tmpfs. The user did not ask for space to be freed, so `go clean` was not run.
**2. Tagged commit.** The plan tags "master HEAD". HEAD at release time was `ead8e42`, the planning-only commit that records the reply. It was committed before the checks, so the checks ran on exactly the commit that was tagged and pushed.
Otherwise the plan was executed as written.
## Known Stubs
None.
## Threat Flags
None. The pushes, the tag, the post and the demo snapshot are the surfaces in the plan's threat register. T-19.8-34 to T-19.8-37 were mitigated as planned:
- a fetch and range classification came before the push
- the push was a fast-forward of exactly the tested, tagged commit
- the token stayed in its file
- TestRoadmapNoClientNames passed before the snapshot commit
## Final docs commit
This SUMMARY's completion commit is **local only**. The plan does not push it, and it is not part of v0.4.2.
## Self-Check: PASSED
Found: cf06185, ead8e42 (local and origin/master), tag object 60b13ae (local and on origin as refs/tags/v0.4.2), sm-demo-app aa4de22 (on origin/master), changelog post (HTTP 200).

View File

@@ -0,0 +1,178 @@
---
phase: 19.8
review: 19.8-REVIEW.md
titles: json
findings:
- id: WR-09
severity: warning
disposition: fixed
title: "Lines printed through a live view after its context is cancelled are silently dropped"
- id: WR-10
severity: warning
disposition: fixed
title: "A delegate killed by a signal prints its full argv, including a --password value, and exits 1"
- id: WR-11
severity: warning
disposition: fixed
title: "The wrong-directory hint leaves secret values that start with \"-\" unmasked"
- id: WR-12
severity: warning
disposition: fixed
title: "migrate:status --json still drops the framework sets that migrate applies"
- id: WR-13
severity: warning
disposition: fixed
title: "migrate reports failure after a successful Migrate when the follow-up read fails"
- id: WR-14
severity: warning
disposition: fixed
title: "Process's plain path prints item names without Sanitize"
- id: IN-10
severity: info
disposition: deferred
title: "console.Error during a live view writes straight to the terminal and tears the view"
- id: IN-11
severity: info
disposition: deferred
title: "A blank line printed during a live view is dropped"
- id: IN-12
severity: info
disposition: deferred
title: "Signal relay edge cases: a signal during view start-up, and Windows"
- id: IN-13
severity: info
disposition: deferred
title: "A delegate's stderr always goes to os.Stderr, bypassing the root's error writer"
- id: IN-14
severity: info
disposition: fixed
title: "The ErrReported doc comment still says the exit code is 1"
- id: IN-15
severity: info
disposition: fixed
title: "runLive builds the bubbletea program before it knows it may run"
- 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: 32
recorded: 2026-10-22T01:19:33.337Z
---
# Phase 19.8: Code Review Disposition
| Finding | Severity | Disposition | Source |
|---------|----------|-------------|--------|
| WR-09 | warning | fixed | fix fce232d (quick 261022-4z8) |
| WR-10 | warning | fixed | fix 17c722d (quick 261022-4z8); the exit-130 suggestion is deferred to todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| WR-11 | warning | fixed | fix 707372b (quick 261022-4z8) |
| WR-12 | warning | fixed | fix 57a849d, reworked in 139f5e3 so that lagoon.Status stays plugin-only and migrate:status adds the framework sets itself (quick 261022-4z8) |
| WR-13 | warning | fixed | fix 0691d26 (quick 261022-4z8) |
| WR-14 | warning | fixed | fix 48e13f9 (quick 261022-4z8) |
| IN-10 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-11 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-12 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-13 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md |
| IN-14 | info | fixed | docs 7c15095 (quick 261022-4z8) |
| IN-15 | info | fixed | fix 1ac75ad (quick 261022-4z8) |
| CR-01 | critical | fixed | fix b077dc3 (19.8-05) (not in the current review) |
| CR-02 | critical | fixed | fix 323413c (19.8-05) (not in the current review) |
| CR-03 | critical | fixed | fix 9c89705 (19.8-05) (not in the current review) |
| WR-01 | warning | fixed | fix d151a25 (19.8-05) (not in the current review) |
| WR-02 | warning | fixed | fix 86e4e27 (19.8-05) (not in the current review) |
| WR-03 | warning | fixed | fix fc7706d (19.8-05) (not in the current review) |
| WR-04 | warning | fixed | fix c586c6f (19.8-05) (not in the current review) |
| WR-05 | warning | fixed | fix a9025c0 (19.8-05) (not in the current review); remainder WR-14 (fix 48e13f9) |
| WR-06 | warning | fixed | fix 39677f1 (19.8-05) (not in the current review) |
| WR-07 | warning | fixed | fix 020fe83 (19.8-05) (not in the current review); remainder WR-12 (fix 57a849d) |
| WR-08 | warning | fixed | fix 9b62285 (19.8-05) (not in the current review); remainder WR-11 (fix 707372b) |
| IN-01 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-02 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-03 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-04 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-05 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-06 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-07 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-08 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
| IN-09 | info | deferred | todo .planning/todos/pending/2026-10-22-console-review-info-findings.md (not in the current review) |
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,253 @@
---
phase: 19.8-console-commands-overhaul
reviewed: 2026-10-22T01:17:37Z
depth: standard
diff_range: dd12382..0ebed68 (gap-closure plan 19.8-05, released as v0.4.2)
files_reviewed: 31
files_reviewed_list:
- cmd/summer/runtime.go
- cmd/summer/runtime_test.go
- docs/console/introduction.md
- docs/console/scaffolding.md
- docs/console/writing-commands.md
- docs/services/logging.md
- docs/setup/upgrading.md
- internal/dev/watch.go
- internal/dev/watch_test.go
- modules/bonfire/errors.go
- modules/bonfire/errors_test.go
- modules/bonfire/execute.go
- modules/bonfire/helpers.go
- modules/bonfire/live.go
- modules/bonfire/live_signal_unix_test.go
- modules/bonfire/live_test.go
- modules/bonfire/output.go
- modules/bonfire/README.md
- modules/bonfire/widgets.go
- modules/bonfire/widgets_test.go
- modules/diary/console.go
- modules/diary/daily.go
- modules/diary/daily_test.go
- modules/diary/diary.go
- modules/diary/README.md
- modules/diary/retire_internal_test.go
- modules/lagoon/commands.go
- modules/lagoon/migrations.go
- modules/lagoon/pending_test.go
- modules/lagoon/README.md
- scripts/check-phase19.8.sh
findings:
critical: 0
warning: 6
info: 6
total: 12
status: issues_found
---
# Phase 19.8: Code Review Report (re-review after 19.8-05)
**Reviewed:** 2026-10-22T01:17:37Z
**Depth:** standard
**Files Reviewed:** 31
**Status:** issues_found
## Summary
This is an incremental re-review of the gap-closure commits `dd12382..HEAD` (b077dc3 to 020fe83, plus the gate and docs commits). I read every listed file in full where it changed. I checked the fixes against the bubbletea v2.1.0 sources (`Program.Send`, `Kill`, `shutdown`, `eventLoop`, `cursedRenderer.insertAbove`) and against pflag v1.0.10. I also ran two scratch programs under a pseudo-terminal (`script -qec`).
Verification:
- `go vet` is clean on the touched packages.
- `go test -short -race` passes for bonfire and diary.
- The new summer and dev tests (`TestDelegate*`, `TestAppStreams`, `TestDevStopWaitsForGracefulShutdown`) pass.
All three critical findings from the first review are fixed. So are WR-01 to WR-06. WR-05, WR-07 and WR-08 are fixed as described, but each leaves a gap of the same kind elsewhere, so they are only partly closed (WR-12, WR-14, WR-10 and WR-11 below).
The fixes introduced no new critical defect. The main new problems are:
- **Lost lines after cancellation.** A live view whose context is cancelled stays registered, so lines printed through it are dropped while `printAbove` reports success. I confirmed this on a pty.
- **Signal-killed delegates.** Since the CR-02 fix, only a signal-killed delegate gets summer's error block. That block repeats the full argv, including a `--password` value, and the exit code is 1.
- **Migration accounting.** `migrate:status --json` still uses its own plugin-only list. `migrate` reports a failure when only the read after a successful `Migrate` fails.
New finding IDs continue after the first review's IDs (WR-09 and up, IN-10 and up). `19.8-REVIEW-DISPOSITION.md` drops a recorded decision when its ID is reused for a different finding, so the old IDs are not reused here.
## Prior Findings: Verified State
| ID | Verified state | Evidence |
|----|----------------|----------|
| CR-01 | **Fixed** | `printAbove` sends one `printLineMessage` per call straight into the program's unbuffered queue while holding `liveMu` (live.go:477-487). The event loop handles it inline (`insertAbove`, bubbletea tea.go:888), so lines keep their order. `finishMsg` is sent under the same lock only after every earlier send has been received (live.go:387-393), so no line is lost when the view quits. On a pty the lines came out in order. `TestPrintAboveOrderedNoLoss` covers spinner, progress, process and a concurrent finish. A remaining gap after context cancellation is WR-09. |
| CR-02 | **Fixed** | `runAppCmd` wraps an exited child as `Reported(WithExitCode(err, code))` (runtime.go:170-176), and `exitCode` honours the code (execute.go:76-85). `TestDelegateExitCodePassthrough` checks that the code is 2 and that summer prints no block of its own. The signal-killed branch has new problems, see WR-10. |
| CR-03 | **Fixed** | `appStreams` gives the child `os.Stdout`/`os.Stderr` on a terminal and `out`/`os.Stderr` otherwise (runtime.go:155-160). The child's stderr no longer lands in summer's stdout, and on a terminal the child sees it and asks the production question. A minor gap is IN-13. |
| WR-01 | **Fixed** | `watchSignals` relays SIGINT/SIGTERM, calls `prog.Kill()` (which restores the cursor) and re-raises the signal (live.go:410-466). `TestLiveViewRestoresTerminalOnSignal` covers both the default action and the command's own handler. Edge cases are in IN-12. |
| WR-02 | **Fixed** | `activeView` records the owner, and `printAbove(c, …)` reroutes only the owner's output (live.go:477-481). The slot is claimed with check-and-set under `liveMu` (live.go:362-368), and cleanup is deferred, so it also runs on a panic (live.go:378-395). |
| WR-03 | **Fixed** | `child.stop` waits `dev.ShutdownGrace` (15s), and summer's delegates use the same constant (watch.go:22-27, 243; runtime.go:185). |
| WR-04 | **Fixed** | A failed open is retried after `dailyRetry` (1 minute) or on the next date, with one warning per date (daily.go:70-99). The test covers both paths. |
| WR-05 | **Partly fixed** | Error blocks (every line, the Try lines and the causes), process-view names and errors, spinner and progress current items, failure lines and table cells now go through `Sanitize`. `Process`'s plain path still prints item names raw (WR-14). The deferred IN-08 (a raw C1 byte passes `Sanitize`'s fast path) now applies to every one of these sinks, not only diary. |
| WR-06 | **Fixed** | Replaced writers are retired: a late record opens the file, appends and closes it again (daily.go:100-104, 166-174). `ApplyGlobals` holds `mu` for its whole run (diary.go:228-240). There is no reentrancy on `mu` from the handler path. |
| WR-07 | **Partly fixed** | `Migrate` and `PendingMigrations` now share `migrationSets`, and the applied lines are computed from the pending list before and after the run. `Status` (used by `migrate:status`, and by its `--json` output) still walks only `plugins` (WR-12). A new error path is in WR-13. |
| WR-08 | **Partly fixed** | `tryArgs` masks `--flag=value` and `--flag value` (errors.go:310-331). A value that starts with `-` is not masked, although pflag accepts it (WR-11). The same argv now leaks through the delegate's signal error (WR-10). |
| IN-01..IN-09 | Deferred (todo `2026-10-22-console-review-info-findings.md`) | Not re-reported. IN-08 is now wider, as noted under WR-05. |
## Narrative Findings (AI reviewer)
## Warnings
### WR-09: Lines printed through a live view after its context is cancelled are silently dropped
**File:** `modules/bonfire/live.go:354-360, 477-487` (with `modules/bonfire/helpers.go:150-170`)
**Issue:** `runLive` passes the command's context to bubbletea (`tea.WithContext(ctx)`). `Process` passes the caller's `ctx`. When that context is cancelled, the program shuts down: `Program.Send` now returns at once because `p.ctx` is done (bubbletea tea.go:1211-1216). However, `activeView` still points at the view until `work` returns. Only the deferred cleanup or the signal relay clears it, and the relay runs only for SIGINT/SIGTERM. Meanwhile:
- `printAbove` hands the line to a dead program and still returns `true`, so the caller never falls back to a plain write.
- This applies to `out.Println`/`Printf` data, to Success/Info/Warning lines on stdout, and to every diary console record routed through `bonfire.PrintAbove`.
- These lines are lost for as long as the current `fn(ctx, item)` keeps running after the cancellation.
- Because the program was killed, the final frame and the failed rows are never rendered either.
**Runtime check:** a `Process` whose first item calls `cancel()`, waits 300 ms and then calls `out.Println("after-cancel-line")` and `bonfire.PrintAbove(...)` printed neither line in 3 of 3 pty runs. `PrintAbove` returned `true`. `out.Error`, which goes to stderr and so bypasses `printAbove`, did print.
This is the CR-01 failure mode (a line reported as printed but never shown) on a new path. It hits any `Process` caller that uses a deadline, a parent cancel or a non-signal shutdown.
**Fix:** Have `printAbove` refuse a program that has stopped, and unregister the view when the program ends on its own:
```go
type liveView struct {
owner *console
prog *tea.Program
done <-chan struct{} // the `finished` channel of runLive
}
func printAbove(owner *console, s string) bool {
liveMu.Lock()
defer liveMu.Unlock()
v := activeView
if v == nil || (owner != nil && v.owner != owner) {
return false
}
select {
case <-v.done: // program gone (ctx cancelled, Run failed): print plainly
activeView = nil
return false
default:
}
v.prog.Send(printLine(strings.TrimSuffix(s, "\n")))
return true
}
```
`Send` can still race with a cancellation that lands between the check and the send. To close that window, also watch `ctx.Done()` in a goroutine that clears `activeView` under `liveMu`. Add a pty or buffer test that cancels the context mid-item and asserts that later lines appear.
### WR-10: A delegate killed by a signal prints its full argv, including a --password value, and exits 1
**File:** `cmd/summer/runtime.go:170-178`
**Issue:** After the CR-02 fix, every child failure that is not a normal exit returns `fmt.Errorf("summer: %s: %w", strings.Join(argv, " "), err)` unmasked. This covers death by a signal (`exit.Exited()` false) and every `cmd.Start` error. `RenderError` passes the message only through `sunscreen.Scrub`, which recognises Bearer, `sk-` and `x-api-key` shapes, so `summer admin:create --email a@b.c --password hunter2` prints `ERROR summer: admin:create --email a@b.c --password hunter2: signal: interrupt` on stderr. It happens when the user presses Ctrl+C, when a CI timeout sends SIGKILL, or on an OOM kill.
This is the leak WR-08 fixed for the wrong-directory hint, on a path that the CR-02 fix now makes the only one that prints summer's own block.
In addition, Ctrl+C is the normal way to stop `summer serve` or `summer queue:work`. Since the WR-01 relay re-raises SIGINT, a child that dies by the signal (any delegate without its own handler, or one interrupted during a live view) now produces a spurious ERROR block and exit 1. Shells and CI expect 130 (128+SIGINT). The docs ("A binary killed by a signal gets an error block from summer and exit code 1") describe this behaviour but do not justify it.
**Fix:** Mask the argv with the same helper the hint uses (export it, for example `bonfire.MaskArgs`), or name only the command word. Map signal deaths to 128+n, and treat SIGINT/SIGTERM as already reported:
```go
var exit *exec.ExitError
if errors.As(err, &exit) {
if ws, ok := exit.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
code := 128 + int(ws.Signal())
if ws.Signal() == syscall.SIGINT || ws.Signal() == syscall.SIGTERM {
return bonfire.Reported(bonfire.WithExitCode(err, code))
}
return bonfire.WithExitCode(fmt.Errorf("summer: %s: %w", argv[0], err), code)
}
…
}
```
### WR-11: The wrong-directory hint leaves secret values that start with "-" unmasked
**File:** `modules/bonfire/errors.go:325-329`
**Issue:** `tryArgs` masks the next argument after `--password` only when it does not start with `-`. pflag takes the next argument as the value of a non-boolean flag whatever its first character is (pflag v1.0.10 flag.go:1020-1023, the `'--flag arg'` branch). So `bin/acme admin:create --email a@b.c --password -Xk9!q` runs with the password `-Xk9!q`, and the Try line then prints it in clear text. A dash is a common first character for generated passwords. The docs (writing-commands.md) promise that such values are masked.
**Fix:** Mask the next argument whenever the flag takes a value. A secret-named long flag in `--name value` form always consumes the next argument, unless that argument is `--` or the flag is known to be boolean:
```go
if i+1 < len(args) && args[i+1] != "--" {
b.WriteString(" ***")
i++
}
```
Add a test case with a value that starts with a dash.
### WR-12: migrate:status --json still drops the framework sets that migrate applies
**File:** `modules/lagoon/migrations.go:141-160`, `modules/lagoon/commands.go:270-289`
**Issue:** WR-07's point was that the commands which report on migrations must read the list `Migrate` applies. `Status` still walks only `plugins`. `migrate:status --json` then emits pending refs only when `ref.Plugin == row.Plugin`. The pending migrations of `summercms.attach`, `summercms.cabana`, the deferred-bindings set and the queue set (all returned by `PendingMigrations`) are therefore silently omitted, both from the JSON (whose example says "pending migrations included") and from the table.
After a framework upgrade that adds a River queue migration, `migrate --pretend` lists it but `migrate:status --json` reports nothing pending. A deploy gate that checks for `"applied": false` passes when it should not.
**Fix:** Build `Status` from `migrationSets(gdb, plugins)` (skipping or including empty plugin sets consistently), so all three commands read one list. Extend `TestMigrateStatusJSON` to assert that a framework set's pending migration appears.
### WR-13: migrate reports failure after a successful Migrate when the follow-up read fails
**File:** `modules/lagoon/commands.go:173-180`
**Issue:** After `Migrate` returns, the command calls `PendingMigrations` again. If that read fails (a dropped connection, a statement timeout, a permission problem on a history table), it returns `errors.Join(migrateErr, err)`, which is non-nil even when `migrateErr` is nil. A migration that was fully applied then:
- prints no applied lines and no `Applied N migrations`;
- exits 1.
A deploy pipeline will treat it as failed and may roll back or alert, even though the schema change is in place.
**Fix:** When `migrateErr == nil`, a failed follow-up read must not turn the run into a failure. Fall back to reporting `pending` as applied, with a warning:
```go
after, err := PendingMigrations(gdb, plugins)
applied := pending
if err == nil {
applied = appliedSince(pending, after)
} else if migrateErr == nil {
out.Warning(fmt.Sprintf("Could not re-read the migration history: %v", err))
} else {
return errors.Join(migrateErr, err)
}
```
### WR-14: Process's plain path prints item names without Sanitize
**File:** `modules/bonfire/helpers.go:183` (also `modules/bonfire/live.go:313-317`, the `operation` label)
**Issue:** The WR-05 fix sanitises item names in the live process view (live.go:292) and in failure lines (`failureText`). The plain path, however, prints a successful item with `out.Success(item)`, and `status` writes it unchanged. The plain path runs whenever stdout is a terminal but the view does not animate: with `-n`, inside another live view (now the normal fallback when `runLive` reports `started == false`), or with a forced color profile. In those cases an item name that holds an escape sequence (a remote file name, an admin-typed title, a post slug) reaches the terminal unchanged. The same item is cleaned on one path and raw on the other.
**Fix:** `out.Success(Sanitize(item))` in `Process`, or sanitise inside `console.status` and `console.Error` so every status line is covered regardless of caller. Add a plain-path case to `TestProcessViewStripsControl`.
## Info
### IN-10: console.Error during a live view writes straight to the terminal and tears the view
**File:** `modules/bonfire/output.go:232-238`
**Issue:** `Error` writes to `c.err`, and `emit` reroutes only writes to `c.out`. When stderr and stdout are the same terminal (the usual case), an `out.Error(...)` from inside `Spinner`, `Progress` or `Process` lands mid-frame. A pty run showed the spinner glyph left on the error line (`⠹ ✗ stderr-error-line`), with the renderer continuing on the next row. diary's console records on stderr do go above the view through `PrintAbove`, so the two stderr paths behave differently.
**Fix:** In `emit`, also reroute `w == c.err` when the console owns the running view and `c.err` is a terminal (`printAbove(c, s)`).
### IN-11: A blank line printed during a live view is dropped
**File:** `modules/bonfire/live.go:485`
**Issue:** `out.Println()` gives `"\n"`. `TrimSuffix` turns that into `""`, and bubbletea's `insertAbove` returns early for an empty string (cursed_renderer.go:776-778). `printAbove` still reports `true`, so the blank separator line is lost. It is printed when no view runs.
**Fix:** Send `" "` (or `"\u200b"`) for an empty block, or skip the trim when the block is only a newline.
### IN-12: Signal relay edge cases: a signal during view start-up, and Windows
**File:** `modules/bonfire/live.go:370-375, 427-436, 454-466`
**Issue:** This comes from reading bubbletea v2.1.0; I did not reproduce it.
1. **Start-up race.** `prog.Run` starts on its own goroutine just before the relay is registered. If a signal arrives before `Run` has created and started its renderer, `Kill` runs `shutdown(true)` while `p.renderer` is still nil. `shutdownOnce` then makes `Run`'s own `shutdown(killed)` a no-op after it has started the renderer and hidden the cursor, so the cursor stays hidden.
2. **Windows.** On Windows, `p.Signal(os.Interrupt)` fails, so `raise` calls `os.Exit(130)`. That skips the command's own handler (for example, `summer dev` stopping its serve child) and its deferred cleanup.
**Fix:** Before killing, wait until the program reports it has started (for example, have the wrapped model's `Init` close a `ready` channel). On platforms where `raise` cannot deliver the signal, return without exiting when another handler is registered.
### IN-13: A delegate's stderr always goes to os.Stderr, bypassing the root's error writer
**File:** `cmd/summer/runtime.go:155-160`
**Issue:** Off a terminal, the child's stdout goes through `out`, but its stderr goes to the real `os.Stderr`, not to the error stream of the root that ran the command. For a summer root built with `NewRootIO(…, errW)` (tests, embedding), the app binary's error block cannot be captured. `TestDelegateExitCodePassthrough` therefore cannot check that the binary's own block was printed.
**Fix:** Pass the root's error writer through (for example, a `bonfire.ErrWriter(out)` accessor), defaulting to `os.Stderr`.
### IN-14: The ErrReported doc comment still says the exit code is 1
**File:** `modules/bonfire/errors.go:73-75`
**Issue:** The comment says "the error block prints nothing and the exit code is 1". With `WithExitCode`, a Reported error can now exit with any code from 1 to 255 (the README was updated, the comment was not).
**Fix:** "… and the exit code is 1, or the code of WithExitCode."
### IN-15: runLive builds the bubbletea program before it knows it may run
**File:** `modules/bonfire/live.go:354-366`
**Issue:** `tea.NewProgram` runs before the slot check. It calls `context.WithCancel(ctx)`, and with `TEA_TRACE` set it opens a trace file. When the slot is taken (`started == false`, now the normal fallback for nested widgets), that cancel is never called. With `Process`'s cancellable `ctx`, the child context stays registered on the parent until the parent ends, and the trace file descriptor leaks. A `Process` nested in a loop accumulates both.
**Fix:** Claim the slot first and construct the program only after `activeView` is set (or call `prog.Kill()` on the losing path).
---
_Reviewed: 2026-10-22T01:17:37Z_
_Reviewer: Claude (msd-code-reviewer)_
_Depth: standard_

View File

@@ -0,0 +1,44 @@
---
status: testing
phase: 19.8-console-commands-overhaul
source: [19.8-VERIFICATION.md]
started: 2026-10-22T02:41:10.440Z
updated: 2026-10-22T02:41:10.440Z
---
## Current Test
number: 1
name: On a real terminal, run 'summer', 'bin/<app>', 'bin/<app> help admin:token:create' and 'bin/<app> bogus'
expected: |
Colors, alignment and sections match the approved D-13 list, D-14 help and D-18 error previews; --no-ansi removes every escape code
awaiting: user response
## Tests
### 1. On a real terminal, run 'summer', 'bin/<app>', 'bin/<app> help admin:token:create' and 'bin/<app> bogus'
expected: Colors, alignment and sections match the approved D-13 list, D-14 help and D-18 error previews; --no-ansi removes every escape code
result: [pending]
### 2. On a real terminal, run a command that uses Spinner, Progress, Process, Choice, Ask, Confirm and Secret (or the throwaway pty program from plan 02); press Ctrl+C during a spinner
expected: SSU-like animation, arrow-key selector with confirmation screen, huh prompts; diary log lines print above the view; after Ctrl+C the cursor is visible and the shell is usable
result: [pending]
### 3. Run 'bin/<app> serve' against a real database on a terminal, then with -q, then with -v, then press Ctrl+C; run 'summer dev' and stop it
expected: A compact startup block (version, env, URL, DB and migration state, plugins, queue, scheduler, mail, cache, realtime, search), then INFO lines with component prefixes. -q shows only warnings. -v adds per-plugin lines. Ctrl+C prints the shutdown report and exits 0
result: [pending]
### 4. Resolve the judgment-tier prohibitions listed under 'Prohibitions' below (the LLM-judge verdicts there are non-authoritative)
expected: Each prohibition is confirmed, or rejected with a reason. The plan 03 'never print a secret' prohibition is no longer flagged as violated: both known leaks (WR-10, WR-11) are fixed and reproduced as fixed with built binaries
result: [pending]
## Summary
total: 4
passed: 0
issues: 0
pending: 4
skipped: 0
blocked: 0
## Gaps

View File

@@ -0,0 +1,482 @@
---
phase: 19.8-console-commands-overhaul
verified: 2026-10-22T02:37:57Z
status: human_needed
score: 57/57 must-haves verified
covered_files:
- ".gitignore"
- ".planning/phases/19.8-console-commands-overhaul/19.8-01-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-01-SUMMARY.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-02-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-02-SUMMARY.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-03-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-03-SUMMARY.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-04-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-04-SUMMARY.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-05-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-06-PLAN.md"
- ".planning/phases/19.8-console-commands-overhaul/19.8-06-SUMMARY.md"
- ".planning/quick/261022-4z8-fix-the-open-19-8-re-review-findings-wr-/261022-4z8-PLAN.md"
- ".planning/quick/261022-4z8-fix-the-open-19-8-re-review-findings-wr-/261022-4z8-SUMMARY.md"
- "README.md"
- "cmd/summer/commands_documented_test.go"
- "cmd/summer/docs.go"
- "cmd/summer/docs_test.go"
- "cmd/summer/main.go"
- "cmd/summer/parity.go"
- "cmd/summer/runtime.go"
- "cmd/summer/runtime_test.go"
- "docs/console/introduction.md"
- "docs/console/scaffolding.md"
- "docs/console/setup-and-maintenance.md"
- "docs/console/utilities.md"
- "docs/console/writing-commands.md"
- "docs/database/migrations.md"
- "docs/examples/blog/console/publish.go"
- "docs/examples/blog/console/publish_test.go"
- "docs/services/configuration.md"
- "docs/services/logging.md"
- "docs/setup/configuration.md"
- "docs/setup/installation.md"
- "docs/setup/porting-a-plugin.md"
- "docs/setup/upgrading.md"
- "examples/hello/hello_test.go"
- "examples/hello/main.go"
- "examples/hello/plugins/greeter/plugin.go"
- "go.mod"
- "go.sum"
- "internal/argmask/argmask.go"
- "internal/build/artifact.go"
- "internal/build/build.go"
- "internal/build/build_test.go"
- "internal/build/manifest.go"
- "internal/build/new.go"
- "internal/build/new_golden_test.go"
- "internal/build/scaffold.go"
- "internal/build/stubs/new.tmpl"
- "internal/build/testdata/new-no-plugin/CLAUDE.md.golden"
- "internal/build/testdata/new/CLAUDE.md.golden"
- "internal/build/testdata/new/config__log.yaml.golden"
- "internal/build/testdata/new/main.go.golden"
- "internal/dev/watch.go"
- "internal/dev/watch_test.go"
- "internal/docsite/check_commands.go"
- "internal/docsite/check_commands_test.go"
- "internal/docsite/checks_test.go"
- "modules/beachcomber/README.md"
- "modules/beachcomber/beachcomber.go"
- "modules/bonfire/README.md"
- "modules/bonfire/command.go"
- "modules/bonfire/docs.go"
- "modules/bonfire/docs_test.go"
- "modules/bonfire/errors.go"
- "modules/bonfire/errors_test.go"
- "modules/bonfire/example_test.go"
- "modules/bonfire/execute.go"
- "modules/bonfire/globals.go"
- "modules/bonfire/globals_test.go"
- "modules/bonfire/golden_test.go"
- "modules/bonfire/help.go"
- "modules/bonfire/help_smoke_test.go"
- "modules/bonfire/helpers.go"
- "modules/bonfire/list.go"
- "modules/bonfire/list_smoke_test.go"
- "modules/bonfire/live.go"
- "modules/bonfire/live_signal_unix_test.go"
- "modules/bonfire/live_test.go"
- "modules/bonfire/output.go"
- "modules/bonfire/prompts.go"
- "modules/bonfire/root.go"
- "modules/bonfire/root_test.go"
- "modules/bonfire/selector.go"
- "modules/bonfire/styles.go"
- "modules/bonfire/testdata/error_chain_verbose.golden"
- "modules/bonfire/testdata/error_hint.golden"
- "modules/bonfire/testdata/error_unknown_command.golden"
- "modules/bonfire/testdata/help_passthrough.golden"
- "modules/bonfire/testdata/help_token_create.golden"
- "modules/bonfire/testdata/list.golden"
- "modules/bonfire/testdata/list.json.golden"
- "modules/bonfire/testdata/list_all.golden"
- "modules/bonfire/testdata/list_styled.golden"
- "modules/bonfire/version.go"
- "modules/bonfire/version_test.go"
- "modules/bonfire/widgets.go"
- "modules/bonfire/widgets_smoke_test.go"
- "modules/bonfire/widgets_test.go"
- "modules/bonfire/wrongdir_smoke_test.go"
- "modules/bouncer/README.md"
- "modules/bouncer/turnstile.go"
- "modules/cabana/README.md"
- "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/cabana/token_json_test.go"
- "modules/conga/README.md"
- "modules/conga/client.go"
- "modules/conga/commands.go"
- "modules/conga/commands_test.go"
- "modules/conga/confirm_linux_test.go"
- "modules/conga/conga.go"
- "modules/conga/logger_smoke_test.go"
- "modules/conga/logger_test.go"
- "modules/conga/schedule_test.go"
- "modules/conga/stats_test.go"
- "modules/conga/worker.go"
- "modules/cooler/README.md"
- "modules/cooler/cooler.go"
- "modules/cooler/memory.go"
- "modules/cooler/ratelimit.go"
- "modules/cooler/rediscache.go"
- "modules/cooler/sitecache.go"
- "modules/diary/README.md"
- "modules/diary/channels.go"
- "modules/diary/component.go"
- "modules/diary/config.go"
- "modules/diary/config_smoke_test.go"
- "modules/diary/config_test.go"
- "modules/diary/console.go"
- "modules/diary/console_test.go"
- "modules/diary/daily.go"
- "modules/diary/daily_test.go"
- "modules/diary/diary.go"
- "modules/diary/diary_test.go"
- "modules/diary/example_test.go"
- "modules/diary/retire_internal_test.go"
- "modules/diary/slogtest_test.go"
- "modules/diary/tracer_smoke_test.go"
- "modules/flare/README.md"
- "modules/flare/commands.go"
- "modules/flare/commands_test.go"
- "modules/flare/flare.go"
- "modules/lagoon/README.md"
- "modules/lagoon/attach/bucket.go"
- "modules/lagoon/attach/thumb.go"
- "modules/lagoon/commands.go"
- "modules/lagoon/commands_flags_test.go"
- "modules/lagoon/connection.go"
- "modules/lagoon/encrypted.go"
- "modules/lagoon/fill.go"
- "modules/lagoon/keygen.go"
- "modules/lagoon/logger_smoke_test.go"
- "modules/lagoon/logger_test.go"
- "modules/lagoon/migrations.go"
- "modules/lagoon/pending_test.go"
- "modules/lagoon/pty_linux_test.go"
- "modules/lagoon/purge.go"
- "modules/lagoon/transaction.go"
- "modules/lighthouse/README.md"
- "modules/lighthouse/centrifugo/client.go"
- "modules/lighthouse/centrifugo/commands.go"
- "modules/lighthouse/lighthouse.go"
- "modules/phrasebook/README.md"
- "modules/phrasebook/translator.go"
- "modules/postcard/README.md"
- "modules/postcard/drivers.go"
- "modules/postcard/mailer.go"
- "modules/sunscreen/README.md"
- "modules/sunscreen/sunscreen.go"
- "modules/sunscreen/sunscreen_test.go"
- "modules/surf/README.md"
- "modules/surf/accesslog.go"
- "modules/surf/accesslog_test.go"
- "modules/surf/routelist_command.go"
- "modules/surf/serve.go"
- "modules/surf/serve_lifecycle_test.go"
- "modules/surf/serve_summary_smoke_test.go"
- "modules/surf/serve_test.go"
- "modules/surf/summary.go"
- "modules/surf/summary_test.go"
- "modules/wristband/README.md"
- "modules/wristband/oidc_token.go"
- "scripts/check-phase19.8.sh"
covered_digest: "v2:sha256:472046bb65cca6ab5a4a3a8b7f02a7c66f6f385e90fbecc1f4a7baf1b80b9f9d"
behavior_unverified: 0
overrides_applied: 0
re_verification:
previous_status: gaps_found
previous_score: 54/57
gaps_closed:
- "CR-01 (D-03, D-27): lines printed while a live view runs appear in order and none is lost when the view ends, including after its context is cancelled (WR-09, fix fce232d)"
- "WR-08 (D-19) and the plan 03 secrets prohibition: secret flag values are masked in the wrong-directory Try line whatever their first character (WR-11, fix 707372b) and in summer's delegate error on a signal death or start failure (WR-10, fix 17c722d), through the shared internal/argmask"
- "WR-07 (D-21, D-04): migrate:status (table and --json) lists the same sets migrationSets gives Migrate, framework sets included (WR-12, fix 57a849d, reworked in 139f5e3 so that lagoon.Status stays plugin-only)"
gaps_remaining: []
regressions: []
human_verification:
- test: "On a real terminal, run 'summer', 'bin/<app>', 'bin/<app> help admin:token:create' and 'bin/<app> bogus'"
expected: "Colors, alignment and sections match the approved D-13 list, D-14 help and D-18 error previews; --no-ansi removes every escape code"
why_human: "Visual layout and palette; the goldens were produced with -update from the code itself, so only a person can compare them with the previews"
- test: "On a real terminal, run a command that uses Spinner, Progress, Process, Choice, Ask, Confirm and Secret (or the throwaway pty program from plan 02); press Ctrl+C during a spinner"
expected: "SSU-like animation, arrow-key selector with confirmation screen, huh prompts; diary log lines print above the view; after Ctrl+C the cursor is visible and the shell is usable"
why_human: "Animation, keyboard interaction and terminal restore cannot be asserted by the short suite (plans 01, 02 and 04 recorded human_judgment: true)"
- test: "Run 'bin/<app> serve' against a real database on a terminal, then with -q, then with -v, then press Ctrl+C; run 'summer dev' and stop it"
expected: "A compact startup block (version, env, URL, DB and migration state, plugins, queue, scheduler, mail, cache, realtime, search), then INFO lines with component prefixes. -q shows only warnings. -v adds per-plugin lines. Ctrl+C prints the shutdown report and exits 0"
why_human: "Readability of the styled block is a visual judgment; TestServeLifecycle (Docker) covers the records and exit code"
- test: "Resolve the judgment-tier prohibitions listed under 'Prohibitions' below (the LLM-judge verdicts there are non-authoritative)"
expected: "Each prohibition is confirmed, or rejected with a reason. The plan 03 'never print a secret' prohibition is no longer flagged as violated: both known leaks (WR-10, WR-11) are fixed and reproduced as fixed with built binaries"
why_human: "Judgment-tier prohibitions need explicit human resolution in interactive verification"
---
# Phase 19.8: Console commands overhaul Verification Report
**Phase Goal:** Console commands in `summer` and in application binaries reach the bar set by Laravel's Artisan. Logging, the shared console layer in `bonfire`, and every command's help and output change; the command logic stays. A user sees what a command did, can ask for more or less of it, and learns from `--help` how to use it without opening the docs.
**Verified:** 2026-10-22T02:37:57Z (initial verification 2026-10-22T01:32:12Z)
**Status:** human_needed
**Re-verification:** Yes, after gap closure (quick task 261022-4z8, commits 7c15095..a48afbd plus follow-ups 139f5e3 and b1fba7e)
**Plain verdict:** The phase goal is achieved. All 57 must-haves (5 roadmap success criteria and 52 plan truths) are verified in the codebase, and the three gaps of the first report are closed. Nothing is missing in the code. The status is `human_needed`, not `passed`, only because the verifier contract allows `passed` only when the human verification list is empty. That list still holds the four checks of the first report: three visual or terminal checks (palette and layout, live widgets and Ctrl+C on a real terminal, the serve startup block) and the explicit resolution of the judgment-tier prohibitions. No `*-UAT.md` exists for this phase, so none of them is settled by recorded evidence. They are confirmation checks, not suspected defects. Once a person has done them, the phase can be marked passed.
**MVP mode note:** ROADMAP marks the phase `mode: mvp`, but the goal is not a user story (`user-story.validate` returns `valid: false`). All six plans recorded the decision not to invent a story. This report applies the standard goal-backward method to the goal and SC-1..SC-5 and does not include a User Flow Coverage table.
## Re-verification (2026-10-22, after quick 261022-4z8)
The SUMMARY claims were not taken as evidence. Each gap was re-checked against the code at HEAD `4141cd9` (the code tree has no uncommitted changes; only foreign planning files are dirty), with the pinning tests run by the verifier and with an independent check using built binaries.
### Commits under review
| Commit | Finding | Gap |
|--------|---------|-----|
| `fce232d` fix(bonfire): end a live view when its context is cancelled and print later lines plainly | WR-09 | 1 |
| `1ac75ad` fix(bonfire): claim the live view slot before building the program | IN-15 | (1, same code) |
| `48e13f9` fix(bonfire): strip control characters from Process's plain item lines and the view's label | WR-14 | follow-up |
| `707372b` fix(bonfire): mask secret flag values that start with a dash | WR-11 | 2 |
| `17c722d` fix(summer): mask secret flag values in a delegate's error | WR-10 | 2 |
| `57a849d` fix(lagoon): migrate:status reads the sets migrate applies | WR-12 | 3 |
| `139f5e3` fix(lagoon): keep Status plugin-only; migrate:status adds the framework sets itself | WR-12 rework | 3 |
| `0691d26` fix(lagoon): warn instead of failing when the re-read after a successful migrate fails | WR-13 | follow-up |
| `7c15095`, `a48afbd`, `b1fba7e` | IN-14 doc comment; disposition and todo records | records |
`19.8-REVIEW-DISPOSITION.md` shows `open: 0`, `total: 32`. WR-09..WR-14, IN-14 and IN-15 are fixed with these hashes; IN-10..IN-13 and the exit-130 suggestion are deferred to `.planning/todos/pending/2026-10-22-console-review-info-findings.md`. The commits are local: 20 commits ahead of the local `origin/master` (`ead8e42`), and no tag contains `fce232d`. So the fixes are not in released v0.4.2 yet. That is a release matter, not a goal gap.
### Gap 1 (WR-09): lines after a cancelled live view. CLOSED
- **Code:** `modules/bonfire/live.go`. `tea.NewProgram` no longer takes the caller's ctx (no `tea.WithContext`). `runLive` claims the slot under `liveMu` before it builds the program (lines 368-382). `watchContext` (lines 426-448) does the following on `ctx.Done()`, all under `liveMu`: it clears `activeView`, sends `finishMsg{err: ctx.Err()}` and waits for `finished`. When the program ends first, the watcher only clears the view. `liveView.done` is the `finished` channel, and `printAbove` (lines 529-546) returns false through `case <-v.done:` for a program that has stopped, and unregisters it. The deferred cleanup's `prog.Send` after the watcher has finished the program cannot block, because bubbletea's Run cancels its own context on return. diary's console handler writes the line itself when `bonfire.PrintAbove` returns false (`modules/diary/console.go:165`).
- **Pinning test:** `TestPrintAboveAfterCancel` cancels in the middle of an item. It asserts that the view is unregistered, that `Println`, `Info` and `PrintAbove` lines each appear exactly once, and that `PrintAbove` reports false. The verifier ran it with the rest of the live-view set (`TestNestedProcessBuildsNoProgram`, `TestProcessViewStripsControl`, `TestPrintAboveOrderedNoLoss`, `TestPrintlnStaysWithItsConsole`, `TestLiveViewSingleOwner`, `TestLiveViewPanicUnregisters`, `TestLiveViewRestoresTerminalOnSignal`) under `-race -count=3`: all PASS.
- **Independent pty reproduction:** this is the same scenario that failed 3 of 3 in the first verification. A scratch probe was built from a `git archive HEAD` export: inside a `bonfire.Process` item it calls `cancel()`, then `out.Println("after-cancel-line")`, then `bonfire.PrintAbove(...)`. It ran 5 times under `script(1)`. In 5 of 5 runs `after-cancel-line` printed once after the last frame and `\e[?25h` (cursor restored), `PrintAbove` returned **false** (so its caller prints the line), and `Process` returned `context canceled`. Before the fix the line was lost and `PrintAbove` returned true.
### Gap 2 (WR-11 and WR-10): secret flag values in clear. CLOSED
- **Code:** `internal/argmask/argmask.go` is the one implementation and imports only `strings`. `Join` masks the argument after a secret-named long flag (`password`, `passwd`, `secret`, `token`, `key`) whatever its first character; only `--` is left alone. It also masks the `--flag=value` form. bonfire's `tryArgs` and the `cd` part of the Try line call `argmask.Join` and `argmask.Quote` (errors.go:288-301); bonfire's old local helpers are gone. `cmd/summer/runtime.go:174` `runAppCmd` builds its error from `argmask.Join(argv)` on every branch, and no `strings.Join(argv` remains. An exit code from the child is still passed through with `Reported(WithExitCode(...))`.
- **Pinning tests (PASS, verifier run):** `TestWrongDirectoryMasksSecrets` (dash-leading cases included); `TestRunAppCmdMasksSecrets/killed_by_a_signal` and `/start_failure`; `TestDelegateExitCodePassthrough/exit_code`, `/killed_by_a_signal`, `/killed_by_a_signal_masks_secrets`.
- **Independent checks with built binaries** (built into the scratchpad from HEAD):
- `cd plugins; ../bin/hello admin:create --email a@b.c --password hunter2` printed `Try: cd ..; bin/hello admin:create --email a@b.c --password ***` (exit 1).
- The same command with `--password -Xk9q` printed `--password ***` (it was in clear text before the fix).
- `summer admin:create --email a@b.c --password hunter2 --api-token=t0k3n --client-secret -Xk9q` was delegated to a fake app binary that kills itself with SIGKILL. It printed `ERROR summer: admin:create --email a@b.c --password *** --api-token=*** --client-secret ***: signal: killed` and exited 1, which is what plan 05's CR-02 truth specifies. With `-v` the chain shows only `signal: killed`.
- A delegate that exits 2 makes summer exit 2 with no second error block, so the CR-02 behavior is unchanged.
### Gap 3 (WR-12): migrate:status --json omits the framework sets. CLOSED
- **Code:** `modules/lagoon/migrations.go`. The new unexported `setStatus` (line 160) takes its sets from `migrationSets(gdb, plugins)`, the same list `Migrate` (line 69) and `PendingMigrations` (line 251) use. `migrate:status` reads `setStatus` (commands.go:282), so every `PendingMigrations` ref, whose `Plugin` field is the set id, matches a row, and framework pending migrations appear with `"applied": false`. The exported `lagoon.Status` is back to its pre-task behavior: the verifier compared it with the `a28957f` version. It has the same plugin-only `HasMigrations` iteration and the same empty row for a plugin without a history table; only the row-reading loop moved into the shared unexported `historyRows`. The exported API diff `a28957f..HEAD` over modules, cmd and internal adds only `argmask.Join` and `argmask.Quote`, which are in `internal/`, so they are not framework API. Nothing was removed or changed.
- **Pinning tests (Docker, PASS, verifier run with `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1`):** `TestMigrateStatusJSON` deletes the newest `summercms.attach` history row and asserts that `--json` (and `--json -q`) lists that migration as pending. It also asserts that the table lists `summercms.attach, summercms.cabana, deferred, queue, demo.alpha, demo.beta` in order. `TestStatusPluginRows` pins the plugin-only `Status`. `TestMigrateRereadFailureWarns`, `TestMigrateAppliedLines`, `TestPendingMigrations`, `TestMigratePretend`, `TestTwoPluginMigrationSetsIsolated` and `TestDocsMigrate` also PASS.
- **Consumer regression check:** `TestRollbackIsolatesFonotekaFullSchema` in `../fonoteka.go/parity` (it calls `lagoon.Status`, `replace => ../summercms.go`) PASSES against HEAD. It failed at `a48afbd`, before the 139f5e3 rework.
### Regression spot-checks (nothing else regressed)
| Check | Result |
|-------|--------|
| `go vet ./...` | PASS |
| `go test -short -count=1 ./...` (all packages, live tree) | PASS, every package ok |
| `scripts/check-phase19.8.sh self-test`, `go`, `hello`, `docs` (live tree, now clean of foreign code edits) | all PASS ("phase19.8 go passed", with race on diary, sunscreen, bonfire and docsite; SUMMER list 7 named tests including TestRunAppCmdMasksSecrets) |
| `PHASE198_REQUIRE_DOCKER=1 scripts/check-phase19.8.sh docker` | PASS (8 lagoon tests including TestMigrateRereadFailureWarns and TestStatusPluginRows; 3 cabana token JSON tests) |
| `summer` bare / `summer bogus` / `summer list --format=json` | exit 0 with the grouped list (59 lines) / ERROR block, `Try: summer list`, exit 2 / 35 commands, 0 without examples |
| `summer help make:command`, `bin/hello help migrate:status` | Description, Usage, Arguments, global options, Help, Examples; migrate:status's Long help names the framework sets |
| `bin/hello key:generate -q`, `bin/hello route:list --json` | key printed, exit 0; JSON route list |
| Debt markers (TBD/FIXME/XXX) in files changed `a28957f..HEAD`; TODO/HACK added | none |
| Generated main / internal/build changed since the first verification | no (`git diff 36e2240..HEAD -- cmd/summer/main.go internal/build` is empty) |
## Goal Achievement
### Roadmap Success Criteria
| # | Success criterion | Status | Evidence |
|---|-------------------|--------|----------|
| SC-1 | `diary` module owns logging (Laravel-shaped keys, stderr/stack/daily/console channels, component loggers), sunscreen on every channel, compat shim, daily files on by default, charm stack with a decision note before code | ✓ VERIFIED | `modules/diary` (3,248 lines): `log.default/level/channels/components` constants (config.go:16-24); `DefaultConfig` is a stack of console and daily (`storage/logs/summer.log`, JSON, warn, 14 days); every leaf goes through `sunscreen.Wrap` (channels.go:117-166). README has a `**Name:**` mnemonic; root README row at line 104. The note commit `e6039b4` is an ancestor of the first go.mod lipgloss commit `e903cc3` (merge-base check). go.mod has lipgloss v2.0.6, bubbletea v2.1.0, bubbles v2.2.1, huh v2.0.3, fang v2.0.1, colorprofile v0.4.3. `go list` shows only bonfire and diary import charm. `sunscreen.InstallDefault` keeps its signature. `summer new` writes `config/log.yaml` (golden). Re-verification: unchanged by the gap-closure commits; diary tests PASS under race. |
| SC-2 | Global flags `-v/-vv/-vvv`, `-q`, `--ansi/--no-ansi`, `-n`, `--env`; verbosity sets the diary level at runtime, so River and plugin logs are hidden or consistent at the default level and shown in full at -vv | ✓ VERIFIED | The flags appear in the help output of both binaries (re-checked). The generated main does `PeekGlobals`, `diary.Bootstrap`, `compass.Open(...Env: g.Env)`, `diary.Setup`, `bonfire.OnGlobals(diary.ApplyGlobals)` (examples/hello/main.go, unchanged). TestVerbosityLevels, TestComponentFloors, TestQuietConsoleOnly, TestRiverLoggerComponent, TestGlobalFlagsAgree and TestCallNoGlobalsHook PASS in the gate's go stage. |
| SC-3 | Every command has a description, long help with examples, documented args and options with defaults; `list` grouped by namespace; `summer help <cmd>` and `<binary> help <cmd>` work; the coverage check fails for the framework and warns in plugins | ✓ VERIFIED | Re-checked: bare `summer` prints the grouped list (exit 0); `summer help make:command` and `bin/hello help migrate:status` print the full help layout; `summer list --format=json` lists 35 commands, none without examples. `cmd/summer/commands_documented_test.go` (`t.Error` per `bonfire.CheckDocs` problem) PASSES in the go stage. |
| SC-4 | Sweep of all commands with the Artisan checklist (`--force`, `--pretend`, progress/tables, `--json`) without breaking names or flags; serve and queue:work print a startup summary and a shutdown report; one-shot commands end with a result line and an exit code | ✓ VERIFIED | As in the first report (migrate `--pretend`/`--force`, `route:list --json`, `key:generate -q`, unknown command exit 2, missing DSN exit 1 with a hint, serve summary and report, 15 plugin repos swept). The edge paths flagged in the first report are now fixed: `migrate:status --json` covers the framework sets, and `migrate` no longer exits 1 after a successful run when only the re-read fails (WR-13). Docker: TestServeLifecycle, TestMigratePretend, TestRollbackPretend, TestDeferredPurgePretend, TestMigrateStatusJSON, TestMigrateAppliedLines, TestMigrateRereadFailureWarns and the cabana token JSON tests PASS. |
| SC-5 | diary and bonfire READMEs, docs console and logging pages, landing site and roadmap deck reflect the new console; unit tests in the last plan | ✓ VERIFIED | As in the first report (5 docs/console pages, the logging page, "Upgrading to 0.4", the landing "Console" card, the deck row in sm-demo-app). The gap-closure commits updated the bonfire and lagoon READMEs, the docs/console pages and docs/database/migrations.md in the same commits (WR-10's README fold-in is recorded as a deviation in the quick SUMMARY). The gate's docs stage PASSES at HEAD (67 files clean, 35 commands with examples). |
### Plan must_haves (merged; 52 truths)
| Plan | Truths | Status | Evidence / notes |
|------|--------|--------|------------------|
| 01 diary | 8 | 8 ✓ | Unchanged since the first report; the diary named tests PASS in the gate's go stage at HEAD. |
| 02 bonfire | 8 | 8 ✓ | Unchanged contract; goldens, globals, help, JSON listing, error block and exit codes, degradation matrix all PASS in the go stage. The Output, Input and Progress interfaces and the Command struct are not touched by the gap-closure commits. |
| 03 sweep | 9 | 9 ✓ | As before. The secrets prohibition is no longer violated by a known path (see Prohibitions). |
| 04 tests | 7 | 7 ✓ | `check-phase19.8.sh` self-test, go, hello, docs and docker stages PASS at HEAD (verifier run). The gate now requires TestNestedProcessBuildsNoProgram, TestPrintAboveAfterCancel, TestRunAppCmdMasksSecrets, TestMigrateRereadFailureWarns and TestStatusPluginRows by name. |
| 05 review fixes | 13 | **13 ✓** (was 10 ✓, 3 partial) | **CR-01** ✓: gap 1 closed (TestPrintAboveAfterCancel plus the pty probe, 5/5). **WR-08** ✓: gap 2 closed (dash-leading values and the delegate error path masked; TestWrongDirectoryMasksSecrets, TestRunAppCmdMasksSecrets, built-binary checks). **WR-07** ✓: gap 3 closed (migrate:status reads migrationSets; TestMigrateStatusJSON with a pending framework migration). The API truth still holds: compared with the first report, the gap-closure diff adds no exported identifier under modules/ and changes none. CR-02, CR-03, WR-01..WR-06 and the commit/RED-replay/disposition truths are still verified (pinning tests PASS; disposition `open: 0`). |
| 06 release | 7 | 7 ✓ | Unchanged: v0.4.2 annotated tag on `ead8e42`, changelog post, deck sync. |
**Score:** 57/57 truths verified (5 SC + 52 plan truths; 0 failed; 0 present-but-behavior-unverified). The three behavior-dependent truths that had failed (CR-01's cancellation/cleanup invariant, the WR-08 masking, the WR-07 status list) each have a passing behavioral test named above, so they count as verified on behavior, not on presence.
### Required Artifacts
| Artifact | Expected | Status | Details |
|----------|----------|--------|---------|
| `.planning/notes/2026-10-21-console-charm-stack.md` | D-02 decision note | ✓ VERIFIED | Committed before any charm import |
| `modules/diary/{diary,config,component,console,daily,channels}.go` | diary module | ✓ VERIFIED | Substantive and wired |
| `modules/bonfire/{globals,execute,list,help,docs,errors,live,helpers,styles,version}.go` | console layer | ✓ VERIFIED | live.go now has the watcher, `liveView.done` and the claim-before-build; helpers.go sanitizes the plain Process path |
| `internal/argmask/argmask.go` | shared secret masking (new) | ✓ VERIFIED | Imported by `modules/bonfire/errors.go` and `cmd/summer/runtime.go`; the only masking implementation |
| `modules/surf/summary.go`, `accesslog.go` | serve summary, report and request log | ✓ VERIFIED | |
| `modules/lagoon/migrations.go` | PendingMigrations, migrationSets, Status | ✓ VERIFIED (was PARTIAL) | `migrationSets(gdb, plugins)` used by Migrate, setStatus and PendingMigrations; exported Status unchanged in behavior |
| `modules/lagoon/commands.go` | migrate, migrate:status | ✓ VERIFIED | `setStatus` for the status command; re-read warning (WR-13) |
| `modules/conga/worker.go` | Worker.Stats | ✓ VERIFIED | |
| `internal/dev/watch.go` | dev runs serve | ✓ VERIFIED | |
| `cmd/summer/commands_documented_test.go` | failing coverage gate | ✓ VERIFIED | |
| `cmd/summer/runtime.go` | delegate exit code, streams, masked errors | ✓ VERIFIED (was PARTIAL) | `argmask.Join(argv)` on every error branch |
| `scripts/check-phase19.8.sh` | fail-closed gate | ✓ VERIFIED | all local stages green at HEAD |
### Key Link Verification
| From | To | Via | Status |
|------|----|-----|--------|
| generated main (internal/build) | diary | `diary.Bootstrap` then `diary.Setup(cfg` | ✓ WIRED |
| generated main | bonfire.Execute / OnGlobals | `os.Exit(run(...))`, `bonfire.OnGlobals(diary.ApplyGlobals)` | ✓ WIRED |
| diary/channels.go | sunscreen | `sunscreen.Wrap(` on every leaf | ✓ WIRED |
| conga/client.go | diary | `diary.For("river")` | ✓ WIRED |
| lagoon/connection.go | diary | `NewSlogLogger(diary.For("db")` | ✓ WIRED |
| diary/console.go | bonfire | `bonfire.PrintAbove(` (console.go:165), plain write on false; `bonfire.Sanitize` | ✓ WIRED (PrintAbove now reports false after a cancellation, so the line is written) |
| surf/accesslog.go | diary | `diary.For("http")` | ✓ WIRED |
| surf/serve.go | lagoon | `lagoon.PendingMigrations(` | ✓ WIRED |
| lagoon/commands.go migrate:status | lagoon/migrations.go migrationSets | `setStatus(gdb, plugins)` | ✓ WIRED (new) |
| modules/bonfire/errors.go | internal/argmask | `argmask.Join` / `argmask.Quote` | ✓ WIRED (new) |
| cmd/summer/runtime.go | internal/argmask | `argmask.Join(argv)` | ✓ WIRED (new) |
| cmd/summer tests | bonfire.CheckDocs | `bonfire.CheckDocs(` | ✓ WIRED |
| cmd/summer/runtime.go | bonfire/execute.go | `Reported(WithExitCode(...))` | ✓ WIRED |
| v0.4.2 tag | changelog | changelog-post.py | ✓ (post id 37, HTTP 200) |
### Data-Flow Trace (Level 4)
| Artifact | Data | Source | Real data | Status |
|----------|------|--------|-----------|--------|
| serve startup summary | migration state, plugins, drivers | `lagoon.PendingMigrations`, `party` plugins, config lookups | yes (TestServeLifecycle with Postgres) | ✓ FLOWING |
| `migrate:status` / `--json` | applied and pending list per set | `setStatus` over `migrationSets` + `PendingMigrations` | yes, framework sets included (TestMigrateStatusJSON with Postgres) | ✓ FLOWING (was PARTIAL) |
| `list --format=json` | command catalog | `bonfire.Describe` over registered commands | yes (35 commands) | ✓ FLOWING |
| http request log | method, pattern, status | `recordPattern` in the route mux | yes (TestAccessLog*) | ✓ FLOWING |
### Behavioral Spot-Checks (verifier-run, re-verification)
| Behavior | Command | Result | Status |
|----------|---------|--------|--------|
| Bare tool prints the Artisan list | `summer --no-ansi` | grouped list, exit 0 | ✓ PASS |
| Per-command help | `summer help make:command`, `bin/hello help migrate:status` | Description, Usage, Arguments, Global options, Help, Examples | ✓ PASS |
| Unknown command is a usage error | `summer bogus` | ERROR block, `Try: summer list`, exit 2 | ✓ PASS |
| JSON catalog complete | `summer list --format=json` | 35 commands, 0 without examples | ✓ PASS |
| -q keeps data | `bin/hello key:generate -q` | key printed, exit 0 | ✓ PASS |
| --json for scripts | `bin/hello route:list --json` | JSON route list | ✓ PASS |
| Wrong-directory hint masks secrets | `cd plugins; ../bin/hello admin:create --email a@b.c --password hunter2` | `--password ***` | ✓ PASS |
| Same with a dash-leading value | `... --password -Xk9q` | `--password ***` | ✓ PASS (was FAIL) |
| Signal-killed delegate masks argv | `summer admin:create ... --password hunter2 --api-token=t0k3n --client-secret -Xk9q` (fake app binary that SIGKILLs itself) | `--password *** --api-token=*** --client-secret ***: signal: killed`, exit 1 | ✓ PASS (new) |
| Delegate exit code passthrough | fake app binary exiting 2 | summer exit 2, no second block | ✓ PASS |
| Lines after a cancelled Process view | scratch probe under `script(1)`, 5 runs | `after-cancel-line` printed once after the last frame in 5/5; PrintAbove returned false (caller writes); cursor restored | ✓ PASS (was FAIL) |
| Pending framework migration in `migrate:status --json` | TestMigrateStatusJSON (Docker) | listed with `applied: false`; the table lists the framework sets first | ✓ PASS (was gap) |
| Exported `lagoon.Status` consumer | `../fonoteka.go` `go test -run TestRollbackIsolatesFonotekaFullSchema ./parity/` (Docker) | ok | ✓ PASS |
### Probe Execution
No `scripts/*/tests/probe-*.sh` exist. The phase's probe is `scripts/check-phase19.8.sh`:
| Stage | Command | Result | Status |
|-------|---------|--------|--------|
| self-test | `bash scripts/check-phase19.8.sh self-test` | "phase19.8 self-test passed" | PASS |
| go | `bash scripts/check-phase19.8.sh go` (live tree; the code tree is clean) | "phase19.8 go passed" (vet, short, race, named tests), exit 0 | PASS |
| hello | `bash scripts/check-phase19.8.sh hello` | "phase19.8 hello passed", exit 0 | PASS |
| docs | `bash scripts/check-phase19.8.sh docs` | "phase19.8 docs passed", exit 0 | PASS |
| docker | `PHASE198_REQUIRE_DOCKER=1 TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh docker` | 8 lagoon and 3 cabana tests passed, exit 0 | PASS |
| plugins, apps, landing | not re-run | These stages touch sibling repos, and another session is working in `../sm-demo-app`. The gap-closure commits do not change the generated main or any exported API, so no plugin or app pin is affected. Plan 06 recorded `--all` green at ead8e42 | NOT RUN by verifier |
### Requirements Coverage
There are no REQUIREMENTS.md IDs for this phase (none orphaned). The plans' `requirements:` fields cite SC-1..SC-5 and D-01..D-30:
| Source | IDs | Status |
|--------|-----|--------|
| CONTEXT decisions | D-01..D-30 | 30/30 honored; D-03, D-19, D-21 and D-27, which the plan 05 gaps touched, are now fully satisfied |
| Plan 01 | SC-1, SC-2, D-01, D-02, D-04, D-05, D-08, D-09, D-10, D-12 | ✓ SATISFIED |
| Plan 02 | SC-2, SC-3, D-01..D-04, D-13..D-18, D-27, D-28, D-30 | ✓ SATISFIED |
| Plan 03 | SC-3, SC-4, SC-5, D-04..D-07, D-11, D-17..D-29 | ✓ SATISFIED |
| Plan 04 | SC-5 and D-03, D-08..D-10, D-12..D-19, D-21, D-23, D-25, D-27..D-29 | ✓ SATISFIED |
| Plan 05 | D-03, D-04, D-10, D-12, D-18, D-19, D-21, D-25, D-27..D-29 | ✓ SATISFIED (was PARTIAL) |
| Plan 06 | D-26 | ✓ SATISFIED |
### Prohibitions
| Plan | Prohibition | Tier | Disposition |
|------|-------------|------|-------------|
| 01 | Only bonfire and diary import charm modules | judgment (checked mechanically) | Holds: `internal/argmask` imports only `strings` |
| 01 | Nothing imports gopkg.in/yaml.v3 | judgment (checked mechanically) | Holds |
| 01-03 | No consuming application named in framework code and docs | judgment | LLM-judge: holds (gate docs stage, 67 files clean at HEAD); non-authoritative |
| 01-06 | Nothing pushed or tagged before the go-ahead; no force-push; no deploy | judgment | LLM-judge: holds (the gap-closure commits are local and untagged); non-authoritative |
| 02 | No method added to Output, Input or Progress | judgment (checked mechanically) | Holds: no exported change in the gap-closure diff |
| 02 | No prompt blocks a non-TTY, -n or -q run | judgment | Holds per TestPromptsNoInteraction, TestConfirmProceed, TestConfirmProductionNonTTYProceeds |
| 03 | No command name or flag removed or renamed | judgment | LLM-judge: holds (migrate:status kept its name, flags, Description and Examples; only its Long help changed); non-authoritative |
| 03 | Summary, hints and logs never print a DSN password, key, token or secret | judgment | LLM-judge: **holds for every known path** (was violated). The wrong-directory hint with a dash-leading value and the delegate signal and start-failure errors now mask through `internal/argmask`; reproduced with built binaries. Non-authoritative |
| 04 | No test skipped or weakened; goldens free of credentials | judgment | LLM-judge: holds. The WR-12 rework returned TestTwoPluginMigrationSetsIsolated and TestDocsMigrate to their pre-57a849d form rather than weakening them; non-authoritative |
| 05 | No exported identifier removed or changed | judgment (checked mechanically) | Holds: `git diff a28957f..HEAD` over modules, cmd and internal (non-test) shows only `argmask.Join` and `argmask.Quote` added, both under `internal/`; `lagoon.Status` keeps its signature and its plugin-only result |
| 05 | WR-01 fixed inside live views; generated main unchanged | judgment | Holds: internal/build and cmd/summer/main.go are unchanged since the first verification |
| 06 | Changelog token never printed | judgment | LLM-judge: holds per SUMMARY; non-authoritative |
### Test Quality Audit
| Test area | Linked | Skipped | Circular | Assertion level | Verdict |
|-----------|--------|---------|----------|-----------------|---------|
| diary (30 named) | SC-1, D-08..D-12 | 1 conditional (root) | no | value/behavioral (slogtest conformance) | OK |
| bonfire goldens | D-13, D-14, D-18 | 0 | partial: created with `-update` | value | OK, provenance PARTIAL (human item 1) |
| bonfire behavior (live, signals, exit codes) | D-03, D-18, D-27 | 0 | no | behavioral (helper processes, pty, cancellation) | OK; cancellation now covered by TestPrintAboveAfterCancel |
| lagoon Docker | D-21, WR-07, WR-12, WR-13 | 0 under Docker | no | value | OK; TestMigrateStatusJSON asserts a pending framework migration |
| cmd/summer runtime | CR-02, CR-03, WR-03, WR-10 | POSIX-only conditionals | no | behavioral | OK; signal-death and start-failure leak cases covered |
Disabled tests on requirements: 0. Circular: 0 blocking. Insufficient assertions: 0. One small note carried from the quick verification: printAbove's "Run ended on its own" branch (not via cancellation) has no committed pinning test; the cancellation branch it shares code with is pinned.
### Anti-Patterns Found
| File | Line | Pattern | Severity | Impact |
|------|------|---------|----------|--------|
| internal/build/artifact.go | 182 | `"TODO: describe " + cmdName` | ℹ️ Info | A template string the scaffolder writes into generated commands (pre-phase) |
| cmd/summer/main.go, runtime.go, runtime_test.go, modules/flare/commands.go | n/a | gofmt drift | ℹ️ Info | Pre-existing, recorded in deferred-items.md and in the quick SUMMARY |
| modules/lagoon/commands.go | migrate:status Description | "Show per-plugin migration history" / example "Show the applied migrations per plugin" | ℹ️ Info | The command now also lists framework sets; the Long help says so, but the one-line description still says per-plugin. Kept unchanged on purpose (P-06); cosmetic |
No TBD, FIXME or XXX in any file this phase or the gap-closure task changed; no TODO or HACK added.
### Human Verification Required
The goal is achieved; these are the confirmation checks from the first report. Three are visual or terminal checks the contract always routes to a person, and one is the explicit resolution of judgment-tier prohibitions. None is a suspected defect.
1. **Styled list, help and error on a real terminal.** Run `summer`, `bin/<app>`, `bin/<app> help admin:token:create` and `bin/<app> bogus`. Expected: the output matches the D-13, D-14 and D-18 previews in palette and alignment. Why human: the goldens came from the code itself.
2. **Live widgets and prompts.** Run Spinner, Progress, Process, Choice, Ask, Confirm and Secret on a pty, then press Ctrl+C during a spinner. Expected: SSU-like look; log lines print above the view; the cursor comes back. Why human: animation and keyboard input. (The cancellation path of gap 1 is now covered by a test and a pty probe; this item is about look and feel.)
3. **serve on a real application.** Run it at the default verbosity, with `-q` and with `-v`, then stop it with Ctrl+C; also run `summer dev`. Expected: a readable startup block, component-prefixed INFO lines, a shutdown report, exit 0. Why human: readability.
4. **Judgment-tier prohibitions.** Confirm or reject the non-authoritative verdicts in the Prohibitions table. The secrets prohibition is no longer flagged as violated.
### Gaps Summary
No gaps remain. All five roadmap success criteria and all 52 plan truths hold in the codebase at HEAD `4141cd9`. The three gaps of the first report were each re-checked in the code, with the named pinning tests, and with an independent run that reproduced the original failure scenario:
1. **Live view lifecycle (WR-09):** a cancelled view now ends with its last frame and is unregistered without waiting for the work function. Later lines print plainly, and `PrintAbove` reports false. The pty probe passed 5/5, where the first verification had failed 3/3.
2. **Argv masking (WR-11, WR-10):** one helper (`internal/argmask`) masks dash-leading values and is used by both the wrong-directory hint and summer's delegate errors. Built binaries no longer print `-Xk9q`, `hunter2` or `t0k3n`.
3. **Migration reporting (WR-12):** `migrate:status` (table and `--json`) reads the same `migrationSets` as `migrate`, so framework sets and their pending migrations are listed. The exported `lagoon.Status` keeps its plugin-only contract, and the fonoteka parity consumer passes.
The follow-up items WR-13, WR-14, IN-14 and IN-15 are fixed too. IN-10..IN-13, IN-01..IN-09 and the exit-130 idea are recorded as deferred in the console review todo. The gap-closure commits are local and unreleased; shipping them needs a v0.4.x tag on the user's go-ahead.
---
_Verified: 2026-10-22T02:37:57Z_
_Verifier: Claude (msd-verifier)_

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,392 @@
---
phase: quick-261022-2zy
plan: 01
quick_id: 261022-2zy
slug: user-and-sociallogin-plugin-security-gap
type: execute
mode: quick
wave: 1
depends_on: []
autonomous: false
requirements:
- 2zy-1 # /register mass assignment (organisation fields, IP columns, account flags)
- 2zy-2 # no Entra tenant ID in sign-in data
- 2zy-3 # no post-login event; listener errors discarded
- 2zy-4 # offboarding: sessions and API tokens survive deactivate/ban, /refresh never reloads the user
- 2zy-5 # stale organisation_role after a membership change
files_modified:
# sm-user-plugin, worked in the sm-demo-app submodule checkout
- ../sm-demo-app/plugins/golem15/user/models/user.go
- ../sm-demo-app/plugins/golem15/user/controllers/registration.go
- ../sm-demo-app/plugins/golem15/user/controllers/api_controller.go
- ../sm-demo-app/plugins/golem15/user/controllers/organisations_admin_controller.go
- ../sm-demo-app/plugins/golem15/user/controllers/users_admin_controller.go
- ../sm-demo-app/plugins/golem15/user/classes/admin_actions.go
- ../sm-demo-app/plugins/golem15/user/classes/admin_actions_test.go
- ../sm-demo-app/plugins/golem15/user/classes/events.go
- ../sm-demo-app/plugins/golem15/user/register_test.go
- ../sm-demo-app/plugins/golem15/user/offboarding_test.go
- ../sm-demo-app/plugins/golem15/user/admin_organisations_edge_test.go
- ../sm-demo-app/plugins/golem15/user/README.md
# sm-sociallogin-plugin, standalone repo
- ../sm-sociallogin-plugin/classes/flow.go
- ../sm-sociallogin-plugin/classes/provider.go
- ../sm-sociallogin-plugin/classes/providers_goth.go
- ../sm-sociallogin-plugin/classes/codes.go
- ../sm-sociallogin-plugin/classes/registration.go
- ../sm-sociallogin-plugin/classes/events.go
- ../sm-sociallogin-plugin/matching_test.go
- ../sm-sociallogin-plugin/providers_goth_test.go
- ../sm-sociallogin-plugin/fakeprovider_test.go
- ../sm-sociallogin-plugin/signin_event_test.go
- ../sm-sociallogin-plugin/README.md
# summercms.go framework (Task 4, after phase 19.8 has released)
- modules/cabana/relation.go
- modules/cabana/relation_field.go
- modules/cabana/crud.go
- modules/cabana/clear_on_change_test.go
- modules/cabana/README.md
- docs/backend/relation-manager.md
# only after the user's go-ahead (Task 5)
- ../sm-demo-app/plugins/golem15/user (submodule pointer)
- ../sm-demo-app/plugins/golem15/sociallogin (submodule pointer)
- ../sm-bm-app/plugins/golem15/user (submodule pointer)
- ../sm-bm-app/plugins/golem15/sociallogin (submodule pointer)
- ../fonoteka.go/plugins/golem15/user (submodule pointer)
- ../sm-grzybyfunkcjonalne-app/plugins/golem15/user (submodule pointer)
- ../sm-summercmsio-app/plugins/golem15/user (submodule pointer)
estimate:
tokens: 220000
raw_tokens: 220000
tasks: 5
confidence: low
must_haves:
truths:
- "/register and controllers.RegisterUser fill only name, surname, email, password, preferred_locale and marketing_consent; body values for organisation_id, organisation_role, created_ip_address, last_ip_address, is_onboarded and must_change_password are ignored, and the IP columns come only from RegisterOptions.IP. models.User.Fillable() is unchanged, so admin forms, form extensions and RegisterUser callers keep working."
- "Deactivation is self-service reversible: a password login or a social sign-in of a deactivated (soft-deleted) account restores it, sends the reactivate mail and issues a fresh working token. Deactivating also ends every earlier session JWT and revokes every personal API token, and those stay dead after the restore."
- "A ban is the hard lock: a banned user (deactivated or not) is refused by password login, by social sign-in on every path (identity, verified email match, link, pending completion) without being restored, and by /refresh; banning ends every earlier session JWT and revokes every personal API token, and unban does not revive them."
- "POST /_user/api/v1/refresh answers 401 {error: Could not refresh token, msg: User not found} for a token whose user is deactivated, banned, or that was issued before the user's tokens_valid_after; a valid refresh still answers 200 and stamps last_seen."
- "A Microsoft (Entra) sign-in stores the tenant ID from the id_token tid claim in the identity's profile_data.tenant_id; other providers' profile_data keeps exactly name, email, avatar."
- "Every successful social sign-in (login, register, link, pending completion) fires classes.SignedInEvent with provider, provider ID, identity row ID, tenant ID, provider email, action and created flag; its listener errors and the user.Register / user.Created listener errors (social and /register) are logged and never fail or undo the sign-in or registration."
- "cabana's RelationContract (hasMany) and FieldRelationContract (belongsTo) accept ClearOnChange; the user plugin uses it so that adding or removing a member in the Organisations members manager, and changing the organisation in the Users form, clears organisation_role unless the same save writes it; deleting an organisation still clears both columns."
artifacts:
- path: ../sm-demo-app/plugins/golem15/user/models/user.go
provides: "RegisterFillable, the explicit registration allow-list"
contains: "func (User) RegisterFillable"
- path: ../sm-demo-app/plugins/golem15/user/classes/admin_actions.go
provides: "RevokeUserAccess; DeactivateUsers and BanUsers revoke sessions and API tokens"
contains: "func RevokeUserAccess"
- path: ../sm-demo-app/plugins/golem15/user/controllers/api_controller.go
provides: "Refresh subject check; /register logs listener errors"
contains: "ThrottleStates"
- path: ../sm-sociallogin-plugin/classes/events.go
provides: "SignedInEvent, SignedInEventName"
contains: "type SignedInEvent struct"
- path: ../sm-sociallogin-plugin/classes/providers_goth.go
provides: "Microsoft code exchange that keeps the id_token and reads tid"
contains: "id_token"
- path: modules/cabana/relation_field.go
provides: "FieldRelationContract.ClearOnChange applied in assignBelongsTo"
contains: "ClearOnChange"
- path: modules/cabana/relation.go
provides: "RelationContract.ClearOnChange applied on hasMany link and unlink"
contains: "ClearOnChange"
key_links:
- from: ../sm-demo-app/plugins/golem15/user/controllers/registration.go
to: ../sm-demo-app/plugins/golem15/user/models/user.go
via: "lagoon.Fill with RegisterFillable() instead of Fillable() at registration.go:73"
pattern: "RegisterFillable\\(\\)"
- from: ../sm-demo-app/plugins/golem15/user/middleware/api_token.go
to: ../sm-demo-app/plugins/golem15/user/classes/admin_actions.go
via: "token.IsUsableAt(now) refuses rows whose revoked_at RevokeUserAccess set"
pattern: "revoked_at"
- from: ../sm-sociallogin-plugin/classes/flow.go
to: ../sm-sociallogin-plugin/classes/events.go
via: "app.Events.Fire(ctx, &SignedInEvent{...}) in signIn and linkIdentity"
pattern: "SignedInEvent\\{"
- from: ../sm-sociallogin-plugin/classes/providers_goth.go
to: ../sm-sociallogin-plugin/classes/flow.go
via: "Profile.TenantID -> upsertIdentity profile_data tenant_id and PendingPayload.TenantID"
pattern: "TenantID"
- from: ../sm-demo-app/plugins/golem15/user/controllers/organisations_admin_controller.go
to: modules/cabana/relation.go
via: "members RelationContract ClearOnChange organisation_role, applied before Save(child) in linkRelated/unlinkRelated"
pattern: "ClearOnChange"
- from: ../sm-demo-app/plugins/golem15/user/controllers/users_admin_controller.go
to: modules/cabana/relation_field.go
via: "organisation FieldRelationContract ClearOnChange organisation_role, applied in assignBelongsTo before the row write"
pattern: "ClearOnChange"
---
<objective>
Close the five security gaps reported while planning sm-servers-app in the core plugins sm-user-plugin and sm-sociallogin-plugin, plus one small additive framework feature in cabana, without breaking any app that uses them (fonoteka.go, sm-bm-app, sm-demo-app, sm-grzybyfunkcjonalne-app, sm-summercmsio-app).
1. (2zy-1) /register mass assignment: organisation fields, the IP columns and account flags.
2. (2zy-2) No Entra tenant ID in sign-in data.
3. (2zy-3) No post-login event; register/created listener errors discarded.
4. (2zy-4) Offboarding: sessions and personal API tokens survive deactivate and ban; /refresh never reloads the user.
5. (2zy-5) Stale organisation_role after a membership change.
Purpose: an application (sm-servers-app first) can let users in through Entra, assign organisations by tenant and email domain, and lock people out with a ban.
Output: local commits in the sm-user-plugin checkout at ../sm-demo-app/plugins/golem15/user, in ../sm-sociallogin-plugin and in summercms.go, each with tests and README/docs in the same commit; then a blocking go-ahead before any push, tag, changelog post or submodule pointer bump.
Decisions (user-confirmed or planner's discretion; the executor does not revisit them):
- OD-1, confirmed by the user: deactivation is self-service and reversible, a ban is the lock. Password /login already restores a deactivated account (WinterCMS parity, session_test.go TestLogin around :140-156), and social sign-in keeps doing the same through controllers.FinishLogin (registration.go:98-123) — no change in sociallogin's checkCanSignIn and TestSoftDeleted stays as it is. What deactivation adds is the end of earlier sessions and personal API tokens: the user signs in again (which reactivates) and gets a fresh token. /refresh refuses deactivated users so a session never slides silently past a deactivation. A ban is refused everywhere: password login (classes.RejectIfThrottled, existing), social sign-in (checkCanSignIn, existing, now pinned for banned and deactivated accounts), /refresh (new), session JWTs and API tokens (revoked at ban, new).
- OD-2, confirmed: the profile_data key is tenant_id, written only when a tenant is known.
- OD-3, decided: cabana gets an additive ClearOnChange []string on RelationContract (hasMany) and on FieldRelationContract (belongsTo), instead of a GORM hook on models.User. Reason: the change is small (three call sites: the hasMany branches of linkRelated and unlinkRelated in modules/cabana/relation.go around :955 and :1250, and assignBelongsTo in modules/cabana/relation_field.go:648-663; plus two boot validations), declarative, a no-op at its zero value, and it covers both admin paths: the members manager saves the child with Save(child) and no controller hook, and the user form assigns the picked organisation after FormBeforeUpdate (modules/cabana/crud.go: formBeforeUpdate, then assignBelongsTo, then Save), so neither path can be handled in the plugin without a model-wide hook. A declarative column list was preferred over new pact hook interfaces because a hook for the belongsTo path would need the old and the new key, which ClearOnChange compares internally. Cost: the user plugin's stale-role commit needs a summercms tag that contains ClearOnChange, so it is the last user-plugin commit and ships in its own tag (see Task 5).
- Registration allow-list (user's "yes add it"): RegisterUser fills from a new explicit list, User.RegisterFillable(): name, surname, email, password (the hash), preferred_locale, marketing_consent. Not settable from a registration body any more: organisation_id, organisation_role (membership is the admin's or the application's), created_ip_address, last_ip_address (server-set from RegisterOptions.IP only; today a body value survives when opts.IP is empty), is_onboarded (application-owned onboarding state), must_change_password (set by the admin and the require-password-change console command). Already outside Fillable and unchanged: is_activated, activated_at, activation_code(_issued_at), reset_password_code(_issued_at), has_self_set_password, tokens_valid_after, email_verified_at, handle, permissions, last_seen. User.Fillable() itself stays as it is: cabana intersects it with the admin form's fields (and an app's form extensions), and no other caller fills it from client input. Verified frontends send only email, password, password_confirmation, name, surname, marketing_consent plus event-only keys (fonoteka vue app stores/auth.ts:370-378, sm-bm-app frontend api/user.ts:29, vue-demo-app useDemoAuth.ts:34), and every RegisterUser caller passes keys inside the new list (fonoteka onboarding.go:102 and console/dev_seed.go:362, sociallogin registration.go:121-129).
- Listener errors are logged, never returned: user.Register / user.Created in /register and in social account creation, and the new sociallogin.SignedIn event. A committed registration or sign-in is never undone by a listener, as in WinterCMS.
- The login event lives in sociallogin (classes.SignedInEvent): provider, identity and tenant are sociallogin concepts. One event covers login, register, link and pending completion; Created=true marks an account created by that sign-in.
- No framework change for tokens or refresh: front-end personal API tokens live in the user plugin (models/api_token.go:28 table user_api_tokens, guard middleware/api_token.go:35-83); cabana's BackendAPIToken (backend_api_tokens) is for backend administrators and untouched. /refresh is the plugin's controllers.Refresh (api_controller.go:178-215, bouncer.Refresh at :193); bouncer.RefreshAudienceFor would demand an aud claim (PHP-era tokens have none) and knows nothing of bans, so the plugin checks the subject itself.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@CLAUDE.md
@.planning/STATE.md
@../sm-demo-app/plugins/golem15/user/README.md
@../sm-sociallogin-plugin/README.md
@modules/cabana/README.md
@docs/backend/relation-manager.md
Working copies (observed at planning time, 2026-10-22):
- sm-user-plugin: ../sm-demo-app/plugins/golem15/user, detached HEAD 9986342 (= tag v0.1.6 = origin/master); local branch master is stale at 24b16b1 (an ancestor). All five apps mount the plugin at 9986342. sm-demo-app's go.work only replaces the module (it is not in the use list), so every go command in this checkout runs with GOWORK=off; the plugin's go.mod line 176 replaces summercms with ../../../../summercms.go, so it builds against summercms.go's working tree, including other sessions' uncommitted edits (verified: GOWORK=off go vet ./... passes).
- sm-sociallogin-plugin: ../sm-sociallogin-plugin on master at 9fd58db (= origin/master), clean. Its tracked go.work replaces sm-user-plugin with ../fonoteka.go/plugins/golem15/user (9986342), so its own tests run against the published user plugin; no sociallogin change depends on new user-plugin API.
- summercms.go: phase 19.8 (console overhaul) is executing. At planning time the working tree had another session's uncommitted edits in modules/lagoon (19.8-05) and planning files; nothing in modules/cabana, modules/pact or docs/backend. Remaining 19.8 plans: 19.8-05 (bonfire, diary, lagoon, cmd/summer, internal/dev, docs/console, docs/services/logging.md: no overlap with Task 4's files) and 19.8-06 (release v0.4.2, whose range check requires origin/master..master to hold only 19.8-05's code commits and planning documents). A cabana commit before 19.8-06 runs would trip that check, hence Task 4's precondition. Latest tag: v0.4.1.
- Do not edit fonoteka.go or other app checkouts before Task 5's go-ahead. In summercms.go and the plugin repos, stage files by explicit path, never git add -A, never touch .planning/STATE.md or state.json by hand, no co-author tags.
Source facts the tasks rely on (file:line in the checkouts above):
- user models/user.go:84-92 Fillable(); controllers/registration.go:43-89 RegisterUser fills at :73 and copies opts.IP into the input map at :67-70 only when non-empty; api_controller.go:672 passes readFields(r) (the raw body). /update (api_controller.go:443+) writes only name, surname, email and does not use Fillable.
- cabana admin fill: fillAllowed (modules/cabana/crud.go:1184-1205) intersects Fillable with the form's writable fields; protectedFillKey (:1407) excludes organisation_id; the user form writes organisation_id through the organisation FieldRelationContract with WritableForeignKey (users_admin_controller.go:370-388). fonoteka sets organisation_id and organisation_role with Updates(map)/Exec (onboarding.go:119-135, invitation_service.go:266 and :413, org_provisioner.go:63-70).
- api_controller.go:685-686 discards FireRegisterEvent / FireCreatedEvent errors; sociallogin classes/registration.go:181-183 does the same.
- Members manager: organisations_admin_controller.go:76-90 hasMany contract on users.organisation_id; docs/backend/relation-manager.md:190 (link adopts children whose ForeignKey is NULL, unlink sets it back to NULL). Known-gap test: admin_organisations_edge_test.go:183-213 TestAdminOrganisationRoleOutlivesMembership (assertions at :195-197 and :199-202). Organisation delete already clears both columns (organisations_admin_controller.go:99-117).
- cabana relation code: RelationContract (modules/cabana/relation.go:38-58), validateRelationContract (:479) and validateHasManyContract (:558), linkRelated (:917-975, hasMany Save at :955), unlinkRelated (:1231-1260, hasMany Save at :1250); FieldRelationContract (relation_field.go:22-45), compileFieldRelation (:205), assignBelongsTo (:648-663); the form save order in crud.go: lagoon.Fill(target, fillAllowed(...), projected, ...), validation, formBeforeUpdate/formBeforeCreate, applyPermissionValues, checkRelationScope, checkRelationLocks, assignBelongsTo, Save/Create. Helpers: fieldByColumn (http.go:1204), setModelColumn (relation.go:1332), modelFields (model_fields.go:28). Existing tests: relation_child_test.go (hasMany CRUD, deferred), phase121_form_test.go:582 TestWritableForeignKeySmoke, phase121_relation_lock_test.go:235/:279, phase121_schema_boot_test.go.
- Deactivation: classes/admin_actions.go:95-113 DeactivateUsers only sets deleted_at; BanUsers :140-168 only writes user_throttle; RestoreUsers :117-134; ThrottleStates :21-57. users_admin_controller.go:629-647 wraps DeactivateUsers and fires DeactivatingEvent. users_admin_controller.go:246 already stamps tokens_valid_after on an admin password reset.
- Sessions: classes/user_lookup.go:35-60 PrincipalByID loads scoped and carries TokensValidAfter (the bouncer jwt guard refuses iat before it, modules/bouncer/jwt.go:172); classes/session_user.go:74 does the same for RequestUser. middleware/api_token.go:61-70 accepts any token row with IsUsableAt and a principal. JWT iat has whole-second precision.
- Refresh: api_controller.go:178-215; the "never loads the user row" comment and TouchLastSeen at :204-212; session_test.go:225 TestRefresh (craftToken, bearer, insertUser helpers in session_test.go). bouncer.ErrSubjectRejected text is "User not found" (modules/bouncer/jwt.go:20,144). golang-jwt/jwt/v5 is already a direct requirement of the user plugin.
- sociallogin classes/flow.go: identityUser :328-338 and userByEmail :272-281 load Unscoped; loginWithIdentity :287-307 calls checkCanSignIn :357-371 (activation, ban) before FinishLogin restores; linkIdentity :128-164 issues its own token; upsertIdentity :385-422 writes profile_data :412-416; signIn :311-324. matching_test.go:365 TestSoftDeleted (restore subtests, kept) and :461 TestInactiveAndBanned (inactive and banned on identity, verified_match, link, pending_completion).
- sociallogin Microsoft: providers_goth.go:72-82 Complete calls goth's sess.Authorize, and goth v1.82.0 providers/azureadv2/session.go Authorize keeps only access/refresh token and expiry (the id_token is dropped; Graph /me has no tenant). newMicrosoft :225-254. Profile classes/provider.go:26-41. PendingPayload classes/codes.go:46-77. providers_goth_test.go:38 TestGothProviders (microsoft fake token at :128-130 and :181). settings_test.go:305 TestErrorMessages scans flowErr keys.
- Tests that start Postgres need Docker and skip under -short. If the Docker socket is denied although the user is in the docker group, run the go test command through sudo -u golem (CLAUDE.md).
</context>
<tasks>
<task type="auto" tdd="true">
<name>Task 1: sm-user-plugin: explicit registration allow-list, server-set IP columns, logged /register listener errors</name>
<files>../sm-demo-app/plugins/golem15/user/models/user.go, ../sm-demo-app/plugins/golem15/user/controllers/registration.go, ../sm-demo-app/plugins/golem15/user/controllers/api_controller.go, ../sm-demo-app/plugins/golem15/user/register_test.go, ../sm-demo-app/plugins/golem15/user/README.md</files>
<precondition>Docker is reachable for testcontainers (directly or through sudo -u golem), and git -C ../sm-demo-app/plugins/golem15/user status --short is empty.</precondition>
<behavior>
- TestRegisterIgnoresProtectedFields: seed an organisation row; POST /_user/api/v1/register with email, password, name, surname, preferred_locale "pl", marketing_consent true, plus organisation_id (the seeded id), organisation_role "owner", created_ip_address and last_ip_address "203.0.113.9", is_onboarded true and must_change_password true, answers 200. The stored user has name, surname, preferred_locale and marketing_consent as sent; organisation_id and organisation_role NULL; is_onboarded and must_change_password false; created_ip_address and last_ip_address equal to the request's client address, not 203.0.113.9.
- Direct controllers.RegisterUser with the same fields: with RegisterOptions.IP "198.51.100.7" both IP columns hold it; with an empty IP both are NULL whatever the fields say; the other columns as above.
- Existing TestRegister, TestRegisterEventPayload (the register event payload still carries the client's extra keys, password and confirmation removed), TestRegisterUserPasswordLess and the sociallogin-shaped RegisterUser calls stay green; the admin Users form tests (admin_users_test.go) stay green, proving Fillable() is untouched.
</behavior>
<action>
Branch setup first (the checkout is detached): in ../sm-demo-app/plugins/golem15/user run git fetch origin, git checkout master and git merge --ff-only origin/master. If origin/master is no longer 9986342, note the new base in the SUMMARY and continue on it. Every go command in this checkout runs with GOWORK=off.
Allow-list (2zy-1): add func (User) RegisterFillable() []string to models/user.go returning name, surname, email, password, preferred_locale, marketing_consent, with a doc comment naming what a registration body may set and listing what it may not (organisation_id, organisation_role, created_ip_address, last_ip_address, is_onboarded, must_change_password; the activation, reset, proof, handle and permission columns are never fillable). Leave Fillable() (:84-92) as it is and rewrite only its doc comment: the mass-assignment backstop of the admin form (cabana intersects it with the form's fields and keeps organisation_id protected) and of trusted application code; registration uses RegisterFillable.
RegisterUser (controllers/registration.go:43-89): fill with user.RegisterFillable() instead of user.Fillable() at :73; drop the copy of opts.IP into the input map (:67-70) and instead, after the fill, set CreatedIPAddress and LastIPAddress on the struct from opts.IP when it is not empty, so a body value can never reach those columns. Keep validation, hashing, PasswordLess, Activate and AssignHandle exactly as now. Rewrite the RegisterUser and RegisterOptions doc comments accordingly (fields can set only the RegisterFillable columns; organisation membership and role are set with an explicit update after creation, as an application's onboarding does; IP is the only source of the IP columns).
Listener errors (2zy-3, user side): at api_controller.go:685-686 replace the discarded results with Error-level logs through the package logger pluginLog (controllers/logger.go): "golem15.user: register listener failed" and "golem15.user: created listener failed", with user_id and err; the registration still succeeds. Update the FireRegisterEvent / FireCreatedEvent doc comments (registration.go:125-150): /register logs that error and continues.
Tests per the behavior block in register_test.go (reuse sessionApp, disableRegisterThrottle, postJSON; read how the harness sets the request's client address and assert against that).
README.md (same commits as the code): the registration part of Features/Usage lists what a /register body can and cannot set and that the IP columns are server-set; the Extension events bullet (:39) says /register logs listener errors and continues and that RegisterEvent.Payload is untrusted client input (it still carries every extra key); the RegisterUser / RegisterOptions / FireRegisterEvent / FireCreatedEvent entries (:170-178) match the new comments; add RegisterFillable to the models API list; update the Testing section.
Commits in the user-plugin checkout, explicit paths, no co-author tags, conventional style as in the repo log: (a) fix: registration fills only an explicit allow-list and never takes the IP columns from the body (models/user.go, registration.go, register_test.go, README); (b) fix: log /register listener failures instead of discarding them (api_controller.go, registration.go comments, README). go vet and the full go test before each commit. Do not push.
</action>
<verify>
<automated>cd /media/nvme/dev/golem15/summercms.io/summercms/sm-demo-app/plugins/golem15/user && GOWORK=off go vet ./... && GOWORK=off go test -count=1 -run 'TestRegister|TestAdminUser' ./... && GOWORK=off go test -count=1 ./... && sed -n '/^func (User) Fillable/,/^}/p' models/user.go | grep -c 'organisation_role'</automated>
</verify>
<done>Registration fills only RegisterFillable columns and takes the IP columns only from RegisterOptions.IP; Fillable() still lists its twelve columns (the grep prints 1); /register listener failures are logged; the whole user-plugin suite is green; two commits on master in the user-plugin checkout; nothing pushed.</done>
</task>
<task type="auto" tdd="true">
<name>Task 2: Offboarding: deactivate and ban end sessions and API tokens, /refresh refuses revoked subjects (user plugin); bans pinned and semantics documented (both plugins)</name>
<files>../sm-demo-app/plugins/golem15/user/classes/admin_actions.go, ../sm-demo-app/plugins/golem15/user/classes/admin_actions_test.go, ../sm-demo-app/plugins/golem15/user/classes/events.go, ../sm-demo-app/plugins/golem15/user/controllers/api_controller.go, ../sm-demo-app/plugins/golem15/user/offboarding_test.go, ../sm-demo-app/plugins/golem15/user/README.md, ../sm-sociallogin-plugin/matching_test.go, ../sm-sociallogin-plugin/README.md</files>
<behavior>
- classes: RevokeUserAccess(ctx, db, ids) stamps users.tokens_valid_after with the current time truncated to whole seconds (soft-deleted rows included) and sets revoked_at on every not-yet-revoked user_api_tokens row of those users, leaving earlier revoked_at values unchanged; an empty list writes nothing. DeactivateUsers and BanUsers call it for every selected user (already deactivated or banned ones included) and return the same counts as before.
- offboarding_test.go, deactivate: a user with a session JWT issued at least one second before the action and a usr_ API token; after the admin deactivate bulk action the JWT is refused by GET /fetch and by POST /refresh (401, error "Could not refresh token"), the API token is refused by the APITokenGuard; a password login right after the deactivation (same second allowed) restores the account, sends the reactivate mail and returns a token that GET /fetch accepts; the old JWT and the old API token stay refused.
- offboarding_test.go, ban: after the ban bulk action the old JWT is refused by /fetch and /refresh and the API token by the guard; password login answers the invalid-login response; after unban the old JWT and API token stay refused and a fresh login works.
- offboarding_test.go, refresh subject checks without RevokeUserAccess: a user soft-deleted directly (deleted_at only) -> refresh 401; a user banned directly through a user_throttle row (no stamp) -> refresh 401; a token whose iat precedes tokens_valid_after set directly -> refresh 401; a suspended (not banned) user -> refresh 200; a normal user -> refresh 200 with last_seen stamped; a refused refresh stamps no last_seen. Existing TestRefresh, TestLogin (restore and reactivate mail) and TestDeactivatingEvent stay green.
- sociallogin TestInactiveAndBanned (matching_test.go:461) gains a banned-and-deactivated user: identity sign-in, verified email match and pending completion all answer the banned message, the account stays soft-deleted, no reactivate mail is sent and no identity row is created for it. TestSoftDeleted (restore of a deactivated, not banned account) stays unchanged and green.
</behavior>
<action>
User plugin, in ../sm-demo-app/plugins/golem15/user (master after Task 1, GOWORK=off):
Add the exported RevokeUserAccess(ctx context.Context, db *gorm.DB, userIDs []uint) error to classes/admin_actions.go with the file's transaction convention (callers pass their transaction; use fresh(ctx, db)). One cutoff for both writes: time.Now() truncated to whole seconds, because JWT iat has whole-second precision and a token minted right after the revocation (a reactivating login) must be valid; a token minted earlier within that same second also stays valid, which the doc comment states. Writes: an unscoped UpdateColumn of users.tokens_valid_after for the ids, and an update of models.APIToken setting revoked_at where user_id is in the ids and revoked_at IS NULL. Doc comment: it ends every session JWT issued before the cutoff (the jwt guard, RequestUser and /refresh honour tokens_valid_after) and every personal API token of the users; applications may call it for their own offboarding, and after writing a ban directly to user_throttle.
DeactivateUsers (:95-113): after the deleted_at writes call RevokeUserAccess for every non-nil selected user with an id, whether deactivated now or before (a repeated deactivate also cleans accounts deactivated before this change); keep the returned count. BanUsers (:140-168): call RevokeUserAccess for all ids after the throttle writes; keep the count. RestoreUsers and UnbanUsers stay unchanged: revoked tokens stay revoked. Update the doc comments of DeactivateUsers, BanUsers and DeactivatingEvent (classes/events.go:59-67: sessions and API tokens are already revoked when it fires).
Refresh (2zy-4) in controllers/api_controller.go:178-215: keep the bouncer.Refresh call and its existing 401 body for token-level failures. After it succeeds, read the presented token's sub and iat with github.com/golang-jwt/jwt/v5 ParseUnverified (bouncer.Refresh already verified signature, audience, refresh window and blacklist), load the user with a scoped query by id through gdb and the request context, and refuse when the user is not found (missing or deactivated: a deactivated user signs in again, which reactivates the account), when TokensValidAfter is set and iat is before it, or when classes.ThrottleStates (with classes.ContextWithThrottle(ctx, app)) reports a ban. A refusal answers 401 with the existing shape: error "Could not refresh token", msg bouncer.ErrSubjectRejected.Error(); a database failure answers writeOpaque500. Suspension is deliberately not checked: it is the failed-password throttle, and refusing refresh on it would let anyone who knows an email log its owner out. Move TouchLastSeen after the checks so a refused refresh stamps nothing; replace the comment at :204-205 with one describing the subject check.
Tests per the behavior block: unit cases in classes/admin_actions_test.go (its Postgres setup); offboarding_test.go in the plugin root with the admin harness for bulk actions (admin_harness_test.go) and the session helpers in session_test.go, plus the API-token guard path used by api_tokens_test.go; capture the reactivate mail the way session_test.go TestLogin does.
User README.md: a short "Deactivation and bans" subsection under Features, and matching edits to the JWT sessions bullet (:20), Last seen (:33), Admin screens (:28) and the Bulk actions row (:129): deactivation is self-service and reversible (a password login or a golem15.sociallogin sign-in restores the account and sends the reactivate mail; an administrator can restore it too), and it ends every earlier session and personal API token, so the user signs in again and gets a fresh token, while old API tokens stay revoked; /refresh refuses deactivated and banned users and tokens from before tokens_valid_after; a ban is the lock: password login, social sign-in and refresh refuse it and its sessions and API tokens are revoked; unban and restore revive no token. Add RevokeUserAccess to the classes API list (:184) and an upgrade note: accounts deactivated or banned before this version keep their API tokens until deactivated or banned again (the actions are idempotent) or RevokeUserAccess runs for them. Update the Testing section.
Commit in the user-plugin checkout: fix: deactivate and ban revoke sessions and API tokens, and /refresh refuses deactivated, banned and cut-off subjects (admin_actions.go, admin_actions_test.go, events.go, api_controller.go, offboarding_test.go, README.md). No push.
Sociallogin, in ../sm-sociallogin-plugin (master, own go.work): no code change. Extend TestInactiveAndBanned with the banned-and-deactivated case per the behavior block (seed it like the banned user plus a soft delete; reuse the trash/restored helpers pattern from TestSoftDeleted). README.md: the matching table row (:49) keeps the restore and the line at :55 becomes: every sign-in requires an activated account without a ban; a deactivated account is restored by a social sign-in exactly as by a golem15.user password login (with the reactivate mail), a banned one is refused on every path and never restored. Add a Security notes bullet stating the same split (deactivation is reversible by its owner; a ban is the lock, checked before any restore or identity write). Update the Testing paragraph.
Commit in ../sm-sociallogin-plugin: test: a banned account is refused and never restored by social sign-in, and the deactivation semantics are documented (matching_test.go, README.md). No push.
</action>
<verify>
<automated>cd /media/nvme/dev/golem15/summercms.io/summercms/sm-demo-app/plugins/golem15/user && GOWORK=off go vet ./... && GOWORK=off go test -count=1 -run 'TestOffboarding|TestRefresh|TestLogin|TestDeactivatingEvent|TestAdminUserActions|TestRevokeUserAccess|TestSharedAPIToken|TestAPIToken' ./... && GOWORK=off go test -count=1 ./... && cd /media/nvme/dev/golem15/summercms.io/summercms/sm-sociallogin-plugin && go vet ./... && go test -count=1 -run 'TestSoftDeleted|TestInactiveAndBanned|TestMatching' ./... && go test -count=1 ./...</automated>
</verify>
<done>Deactivate and ban revoke earlier session JWTs and personal API tokens; a deactivated user reactivates by signing in and gets a working token; a banned user is refused by login, social sign-in and refresh and is never restored; /refresh refuses deactivated, banned and cut-off subjects with the existing 401 body; both suites green; one commit in each repo; nothing pushed.</done>
</task>
<task type="auto" tdd="true">
<name>Task 3: sm-sociallogin-plugin: Entra tenant ID in profile_data, SignedInEvent after every social sign-in, logged listener errors</name>
<files>../sm-sociallogin-plugin/classes/provider.go, ../sm-sociallogin-plugin/classes/providers_goth.go, ../sm-sociallogin-plugin/classes/codes.go, ../sm-sociallogin-plugin/classes/flow.go, ../sm-sociallogin-plugin/classes/registration.go, ../sm-sociallogin-plugin/classes/events.go, ../sm-sociallogin-plugin/providers_goth_test.go, ../sm-sociallogin-plugin/fakeprovider_test.go, ../sm-sociallogin-plugin/signin_event_test.go, ../sm-sociallogin-plugin/README.md</files>
<behavior>
- TestGothProviders microsoft (providers_goth_test.go): the fake token answer carries an id_token whose payload has aud = the client id and tid = a GUID; Complete returns Profile.TenantID = that GUID in lower case. Without id_token, with a malformed id_token, with an aud that is not the client id, or with a tid that is not a GUID, TenantID is empty and the sign-in still succeeds. invalid_grant still maps to ErrCodeExpired; no request leaves the provider's hosts; the timeout case holds; the authorization URL cases are unchanged.
- signin_event_test.go (Postgres, fake provider with a TenantID): an identity sign-in fires one SignedInEvent with User, Provider "fake", ProviderID, IdentityID (the golem15 oauth identity row id), TenantID, Email (provider email), Action "login", Created false; a register-action creation fires it with Action "register", Created true; a link fires it with Action "link"; a pending completion fires it once; a sign-in that restores a deactivated account fires it once with Action "login".
- signin_event_test.go: profile_data holds tenant_id for a profile with a tenant and exactly name, email, avatar without one; a pending sign-in keeps the tenant through PendingPayload (tenant_id in the stored payload, omitted when empty) and the completed identity has profile_data.tenant_id.
- signin_event_test.go: a SignedInEvent listener returning an error does not fail the sign-in (token and user payload returned) and the error is logged; a listener that changes the user's name in the database is reflected in the returned user payload; a register/created listener returning an error does not fail social account creation and is logged.
</behavior>
<action>
All work in ../sm-sociallogin-plugin on master (after Task 2's commit). Keep the plugin's logging style (slog.Default(), as in controllers/*.go): sociallogin requires summercms v0.3.1, so do not use diary.
Tenant ID (2zy-2, key tenant_id per OD-2). Add TenantID string to Profile (classes/provider.go:26-41): the directory tenant the account signed in from (Microsoft Entra tid), empty for providers without tenants. Add TenantID to PendingPayload (classes/codes.go:46-59) with JSON key tenant_id and omitempty; copy it in pendingOutcome (flow.go:233-252) and back in PendingPayload.Profile() (codes.go:63-77). In upsertIdentity (flow.go:385-422) add "tenant_id" to profile_data only when p.TenantID is not empty, so other providers keep exactly name, email, avatar (profile_data is json:"-" on the user plugin's OAuthIdentity, so no API response changes).
Microsoft exchange (classes/providers_goth.go). goth's azureadv2 Session.Authorize drops the id_token, so the microsoft provider performs the code exchange itself and keeps goth for the authorization URL and the Graph profile. Give gothProvider an optional complete override (a func field taking ctx, the provider, the goth session string and the callback values, returning Profile and error); Complete (:72-82) delegates to it when set, so google, facebook and github are untouched. newMicrosoft (:225-254) builds an oauth2.Config identical to goth's: ClientID, ClientSecret, RedirectURL from cfg; AuthURL https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize and TokenURL https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token with the same tenant default (DefaultTenant); scopes from the same azureadv2 ScopeType constants converted with string(). The override exchanges the callback's code with context.WithValue(ctx, oauth2.HTTPClient, p.client); maps a failure through p.exchangeError (invalid_grant still becomes ErrCodeExpired, no response body in the error); refuses a token that is not Valid(); builds an azureadv2.Session with AccessToken, RefreshToken and ExpiresAt from the token; calls p.gp.FetchUser with it and passes the result through the existing microsoft profile mapping; then sets TenantID from token.Extra("id_token"). Add a helper (for example entraTenantID(idToken, clientID string) string) that splits the compact JWT into three parts, base64url-decodes the payload (raw URL encoding, tolerating padding), reads the string claims tid and aud, and returns the lower-cased tid only when aud equals clientID and tid is a canonical GUID (8-4-4-4-12 hex), otherwise the empty string. The signature is not verified: the id_token comes straight from the token endpoint over TLS in a client-authenticated back-channel exchange (OpenID Connect Core 3.1.3.7); write that reason in the helper's doc comment. Never log the id_token, the access token or the code. Update newMicrosoft's doc comment.
SignedInEvent (2zy-3). Create classes/events.go with SignedInEventName = "sociallogin.SignedIn" and type SignedInEvent struct with User (*usermodels.User), Provider (registry name), ProviderID (the identity's provider_id), IdentityID (uint, the golem15 oauth identity row id), TenantID, Email (provider-reported, possibly empty), Action (ActionLogin, ActionRegister or ActionLink) and Created (true when this sign-in created the account); doc comment: fired after every successful social sign-in, after the identity is stored and FinishLogin ran (so a deactivated account is already restored), before the session token is minted, outside any transaction; listener errors are logged and never fail the sign-in; the user row is re-read afterwards so listener writes reach the token payload. Make upsertIdentity return the identity row id with the error. Change signIn (flow.go:311-324) to receive the provider, the profile, the identity id and the created flag: after FinishLogin it fires the event through app.Events (skipped when app or app.Events is nil), logs a listener error with slog.Default().Error("golem15.sociallogin: sign-in listener failed", "provider", ..., "user_id", ..., "err", ...), then re-reads the user row by id (scoped; any read failure, including a row a listener removed, is returned as an internal error), then IssueToken and APIArray as now. loginWithIdentity (:287-307) and createUser (registration.go:112-185; Created true, Action register) pass the values through; linkIdentity (:128-164) fires the event with Action link and Created false after its transaction and re-reads the user the same way before IssueToken. In createUser replace the discarded FireRegisterEvent / FireCreatedEvent results (registration.go:181-183) with the same Error-level logging ("register listener failed", "created listener failed") and keep the comment that a committed registration is never undone.
Tests per the behavior block: extend the microsoft cases in providers_goth_test.go (build the unsigned id_token in the test from base64url-encoded JSON header and payload plus any signature segment; the fake token bodies at :128-130 and :181 gain an id_token field); extend fakeprovider_test.go so a fake profile can carry a TenantID; add signin_event_test.go with a listener on the app's event bus registered the way existing tests do (search this repo's and the user plugin's tests for event listener registration) and a slog handler swap to assert the logged failure.
README.md: Features gains the Microsoft tenant line (profile_data.tenant_id from the id_token tid claim, only when known; other providers unchanged) and an Events subsection documenting SignedInEvent / SignedInEventName with every field, when it fires (login, register, link, pending completion, including a restoring sign-in), Created, the re-read, and that listener errors (and user.Register / user.Created listener errors during social registration) are logged and never fail the sign-in; the Account matching section notes that the pending payload keeps the tenant; Security notes gain the tid trust basis (back-channel id_token, aud must equal the client id, GUID format, signature not checked and why) and the advice to match TenantID together with Provider "microsoft" when assigning organisations by tenant; the API reference adds SignedInEvent, SignedInEventName, Profile.TenantID and PendingPayload.TenantID; the Testing paragraph lists the new tests.
Commits in ../sm-sociallogin-plugin, explicit paths, no co-author tags: (a) feat: record the Entra tenant ID of a Microsoft sign-in in profile_data (provider.go, providers_goth.go, codes.go, flow.go tenant parts, providers_goth_test.go, fakeprovider_test.go, README); keep tenant assertions that need the event out of this commit; (b) feat: fire SignedInEvent after every social sign-in and log listener failures (events.go, flow.go, registration.go, signin_event_test.go, README). No push.
Cross-check both plugins together (no repo is modified): write a go.work in your scratchpad directory with go 1.27.0, a use directive with the absolute path of ../sm-sociallogin-plugin, and replace directives sending git.golem15.com/golem15/summercms to the absolute path of summercms.go and git.golem15.com/golem15/sm-user-plugin to the absolute path of ../sm-demo-app/plugins/golem15/user; run the sociallogin suite with GOWORK set to that file and report the result in the SUMMARY.
</action>
<verify>
<automated>cd /media/nvme/dev/golem15/summercms.io/summercms/sm-sociallogin-plugin && go vet ./... && go test -count=1 -run 'TestGothProviders|TestSignedInEvent|TestSoftDeleted|TestInactiveAndBanned|TestMatching|TestRegisterComplete|TestLink|TestErrorMessages' ./... && go test -count=1 ./... && grep -c 'tenant_id' README.md</automated>
</verify>
<done>Microsoft sign-ins store profile_data.tenant_id from a validated tid; SignedInEvent fires once per successful login, register, link and pending completion with every field; listener errors are logged and never fail a sign-in; the sociallogin suite is green against the published user plugin (its go.work) and against the Task 1-2 user plugin (scratch GOWORK); two commits; nothing pushed.</done>
</task>
<task type="auto" tdd="true">
<name>Task 4: cabana ClearOnChange (summercms.go) and the user plugin's stale-role fix on top of it</name>
<files>modules/cabana/relation.go, modules/cabana/relation_field.go, modules/cabana/crud.go, modules/cabana/clear_on_change_test.go, modules/cabana/README.md, docs/backend/relation-manager.md, ../sm-demo-app/plugins/golem15/user/controllers/organisations_admin_controller.go, ../sm-demo-app/plugins/golem15/user/controllers/users_admin_controller.go, ../sm-demo-app/plugins/golem15/user/admin_organisations_edge_test.go, ../sm-demo-app/plugins/golem15/user/README.md</files>
<precondition>Phase 19.8 has released (summercms.go tag v0.4.2 exists and 19.8-06 is complete), or the user has explicitly allowed this cabana commit into the 19.8 release range; and git status --porcelain -- modules/cabana modules/pact docs/backend in summercms.go is empty (no other session editing those paths).</precondition>
<behavior>
- clear_on_change_test.go, hasMany (Postgres, the relation_child_test.go fixtures): a RelationContract with ClearOnChange naming a nullable child column; link sets the foreign key and clears that column on the linked child; unlink clears the foreign key and the column; a contract without ClearOnChange leaves the column untouched (existing behaviour); a deferred link committed by the parent's first save clears it too.
- clear_on_change_test.go, belongsTo form field: a FieldRelationContract with ClearOnChange; an update that changes the key (to another record, and to empty) clears the column; an update that keeps the key leaves it; an update that changes the key and also submits the column through its own form field keeps the submitted value; a create that sets the key and does not submit the column clears it.
- clear_on_change_test.go, boot: ClearOnChange on a belongsToMany field contract or a belongsToMany relation contract, a column the model does not have, a non-pointer (not nullable) column, the ForeignKey itself, or the primary key each stop the start-up with an error naming the controller and the column.
- user plugin admin_organisations_edge_test.go:183-213 flipped and renamed TestAdminOrganisationMembershipClearsRole: after the members-manager unlink both organisation_id and organisation_role are NULL; after linking into the second organisation the role stays NULL; an organisation-less user with a leftover role, linked through the members manager, ends with role NULL; changing a user's organisation in the Users form to another organisation, and to none, clears the role; a Users form save that keeps the organisation keeps the role; deleting an organisation still clears both (existing part).
</behavior>
<action>
Framework, in summercms.go (OD-3). Add ClearOnChange []string to cabana.RelationContract (modules/cabana/relation.go:38-58) and to cabana.FieldRelationContract (modules/cabana/relation_field.go:22-45), each with a doc comment: nullable columns of the related child (hasMany) or of the record (belongsTo) set to NULL whenever this relation changes the foreign key, so values that only make sense for one parent (a role in an organisation, for example) never move to another or outlive the membership; the zero value changes nothing.
Validation at boot: in validateHasManyContract / validateRelationContract (relation.go:479 and :558) and in compileFieldRelation (relation_field.go:205), refuse ClearOnChange on a belongsToMany contract, and refuse any listed column that the model does not have (fieldByColumn over modelFields), whose field is not a pointer, or that is the contract's ForeignKey or the model's primary key; the error names the controller and the column, in the style of the existing contract errors.
Application: in linkRelated's hasMany branch (relation.go:917-975, before the Save at :955) and in unlinkRelated's hasMany branch (:1231-1260, before the Save at :1250), after setModelColumn sets the foreign key, set each ClearOnChange column of the child to nil with setModelColumn (link and unlink always change the key: link adopts only children whose key is NULL, unlink only children of this parent). In assignBelongsTo (relation_field.go:648-663), before setting the foreign key, read the current key value of the model (fieldByColumn; nil pointer or zero counts as empty) and compare it with the submitted id (or empty); when they differ, set each ClearOnChange column to nil unless the same save writes that column through its own form field. To know that, assignBelongsTo receives the set of fill keys this save wrote: the keys of fillAllowed(cc, target, op) that are present in projected, computed in the save function in crud.go where both are in scope; pass it from the single assignBelongsTo call site in crud.go. No other behaviour changes; the deferred unlink of a not-yet-saved parent writes no child row and needs nothing.
Tests: modules/cabana/clear_on_change_test.go per the behavior block, reusing the Postgres fixtures and helpers of relation_child_test.go / phase122_fixture_test.go and the form-save helpers of phase121_form_test.go (its TestWritableForeignKeySmoke is the closest model); boot cases next to the style of phase121_schema_boot_test.go. Docker-backed parts skip under -short like their neighbours.
Docs in the same commit (CLAUDE.md): modules/cabana/README.md, the relations sentence in Features (around :28) and the API reference rows for FieldRelationContract and RelationContract (around :272 and :275) mention ClearOnChange; docs/backend/relation-manager.md gets a short section after the WritableForeignKey paragraph (around :52) for belongsTo fields and a sentence in the hasMany part (around :117-133 and the link/unlink description at :190), with an organisation membership and role as the example and the "unless the same save writes it" rule. If you add a code example through an src= snippet, put it in an example_*_test.go and run summer docs:sync. Run go run ./cmd/summer docs:build --check and go test -run TestDocsTree ./cmd/summer.
Commit in summercms.go, explicit paths only (other sessions have uncommitted files there; leave them alone), no co-author tags: feat(cabana): ClearOnChange clears dependent columns when a relation changes a foreign key (relation.go, relation_field.go, crud.go, clear_on_change_test.go, README.md, docs/backend/relation-manager.md). go vet ./... and go test -short ./... green (and go test ./modules/cabana/... with Docker); if a failure comes only from another session's uncommitted files, record it in the SUMMARY and do not touch those files. Do not push or tag.
User plugin, in ../sm-demo-app/plugins/golem15/user (master after Task 2, GOWORK=off; it builds against summercms.go's working tree through the go.mod replace, so it sees the commit above). Set ClearOnChange: organisation_role on the members RelationContract (organisations_admin_controller.go:76-90) and on the organisation FieldRelationContract (users_admin_controller.go:370-388). Update the doc comments at organisations_admin_controller.go:21-27 ("sets and clears users.organisation_id and nothing else") and :73-75, and at users_admin_controller.go:364-369: adding or removing a member, and changing the organisation in the user form, clear organisation_role unless the same save writes it. Flip and rename the known-gap test and add the user-form cases per the behavior block (adminEnv harness: newAdminEnv, admin, call/expect, seedUser, reload; find the users update route in admin_users_test.go), and replace the test's doc comment (12.1-SECURITY-REVIEW open item 6 is closed). README.md: the Organisations bullet (:26) and the Organisations screen bullet (:30) state the role rule; add a Dependencies note that this behaviour needs a summercms version with cabana ClearOnChange (the tag decided at Task 5); update the Testing section. Leave go.mod's summercms require as it is in this commit: the require bump to the released tag is a separate commit at Task 5, once the tag exists.
Commit in the user-plugin checkout: fix: clear a stale organisation_role when a user's organisation changes in the admin (organisations_admin_controller.go, users_admin_controller.go, admin_organisations_edge_test.go, README.md). This must be the last commit on the user plugin's master, so the earlier fixes can be tagged without it. No push.
</action>
<verify>
<automated>cd /media/nvme/dev/golem15/summercms.io/summercms/summercms.go && go vet ./... && go test -count=1 -run 'TestClearOnChange|TestRelationChild|TestWritableForeignKey' ./modules/cabana/... && go test -count=1 -short ./... && go test -count=1 -run '^TestDocsTree$' ./cmd/summer && go run ./cmd/summer docs:build --check && cd /media/nvme/dev/golem15/summercms.io/summercms/sm-demo-app/plugins/golem15/user && GOWORK=off go vet ./... && GOWORK=off go test -count=1 -run 'TestAdminOrganisation|TestAdminUser' ./... && GOWORK=off go test -count=1 ./...</automated>
</verify>
<done>cabana.RelationContract and cabana.FieldRelationContract accept ClearOnChange with boot validation, documented in the cabana README and docs/backend/relation-manager.md, tested on Postgres; the user plugin clears organisation_role on member add/remove and on a user-form organisation change; the known-gap test asserts the closed gap; framework short suite, docs checker and user-plugin suite green; one commit in summercms.go and one (last) commit in the user plugin; nothing pushed or tagged.</done>
</task>
<task type="checkpoint:decision" gate="blocking">
<name>Task 5: Go-ahead for push, tags, changelog and submodule pointer bumps</name>
<decision>Whether and how far to publish: the summercms.go cabana commit, the user-plugin commits (two releasable sets), the sociallogin commits, and the submodule pointers in the apps.</decision>
<context>
Local commits after Tasks 1-4. sm-user-plugin master (../sm-demo-app/plugins/golem15/user): Task 1 (two commits) and Task 2 (one) need only summercms v0.4.0; Task 4's commit, the last one, needs a summercms tag containing ClearOnChange. sm-sociallogin-plugin master: Task 2 (one) and Task 3 (two); go.mod unchanged (still requires sm-user-plugin v0.1.5 and summercms v0.3.1). summercms.go master: one cabana commit after the 19.8 release. Review with git log --oneline origin/master..master in each repo.
Publishing steps for publish-all, in this order (CLAUDE.md: framework and plugins first, then each app's pointer bump as its own commit; ssu:status / ssu:push can drive the submodule parts):
(1) summercms.go: git fetch, confirm origin/master..master holds the cabana commit (plus planning docs), vet, short suite and docs checker green, push master, annotated tag v0.4.3 (proposed: the tag after 19.8's v0.4.2), push the tag, and the same-day changelog post from ../sm-summercmsio-app with scripts/changelog-post.py (source, then publish with user-facing notes; never print the token).
(2) sm-user-plugin: tag v0.1.7 at the Task 2 commit (registration allow-list, listener logging, offboarding; compatible with summercms v0.4.0); then a commit "chore: require summercms v0.4.3" (go mod edit of the require, then GOWORK=off go mod tidy once the tag is fetchable, tests green), tag v0.1.8 there (adds the stale-role fix); push master and both tags.
(3) sm-sociallogin-plugin: push master, tag v0.1.3, push the tag.
(4) Apps, one commit per app and plugin, staging only the pointer and go.mod/go.sum: the user plugin in fonoteka.go, sm-bm-app, sm-demo-app, sm-grzybyfunkcjonalne-app and sm-summercmsio-app; sociallogin in sm-bm-app and sm-demo-app. An app moving to user v0.1.8 also needs summercms v0.4.3 or newer (bump its summercms require where it pins one); an app that cannot move its framework yet takes v0.1.7. Run each app's go build ./... before committing; leave other sessions' edits alone; push each app.
</context>
<options>
<option id="publish-all">
<name>Release the framework (v0.4.3 + changelog), both user-plugin tags, sociallogin, then bump every app</name>
<pros>All fixes, the stale-role cleanup included, reach every app.</pros>
<cons>Needs 19.8 released first and a framework bump in each app taking v0.1.8; touches five app repos where other sessions may be working.</cons>
</option>
<option id="plugins-first">
<name>Push the user plugin up to the Task 2 commit only (git push origin with that commit as master), tag v0.1.7; push and tag sociallogin v0.1.3; keep the framework and stale-role commits local</name>
<pros>The register, offboarding, tenant and event fixes ship now with no framework release.</pros>
<cons>The stale-role fix waits for the framework release; app pointer bumps are still separate decisions.</cons>
</option>
<option id="hold">
<name>Keep everything local</name>
<pros>Nothing leaves the machine; the plan can still change.</pros>
<cons>The gaps stay open in every app.</cons>
</option>
</options>
<resume-signal>Select: publish-all (optionally naming which apps), plugins-first, or hold.</resume-signal>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| anonymous client -> POST /_user/api/v1/register | untrusted JSON body is mass-assigned into the users row |
| session JWT holder -> POST /_user/api/v1/refresh | a token of a user who has since been deactivated, banned or cut off |
| personal API token holder -> plugin APIs (APITokenGuard) | long-lived secret of a user who has since been deactivated or banned |
| OAuth provider (Microsoft token endpoint, Graph) -> sociallogin | identity, email and tenant asserted by the provider |
| administrator -> cabana Organisations / Users screens | membership changes that must not carry privileges across organisations |
| sociallogin -> application listeners | plugin code reacting to sign-ins; failures must not change the sign-in outcome |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-2zy-01 | E | /register via RegisterUser mass assignment (organisation fields, is_onboarded, must_change_password) | critical | mitigate | Task 1: explicit RegisterFillable allow-list; TestRegisterIgnoresProtectedFields over HTTP and RegisterUser |
| T-2zy-02 | S | created_ip_address / last_ip_address from the body when RegisterOptions.IP is empty | low | mitigate | Task 1: IP columns set only from RegisterOptions.IP after the fill |
| T-2zy-03 | E | organisation_role kept after a membership change (members manager, Users form) | high | mitigate | Task 4: cabana ClearOnChange on both contracts; flipped known-gap test plus form cases |
| T-2zy-04 | E | controllers.Refresh sliding sessions of deactivated, banned or cut-off users | high | mitigate | Task 2: subject check after bouncer.Refresh (scoped load, tokens_valid_after, ThrottleStates ban); offboarding_test.go |
| T-2zy-05 | E | session JWTs and user_api_tokens surviving deactivate/ban and reviving on restore/unban | high | mitigate | Task 2: RevokeUserAccess from DeactivateUsers and BanUsers; restore/unban revive nothing |
| T-2zy-06 | E | a banned account restored or signed in socially | high | mitigate | Existing checkCanSignIn ban check runs before FinishLogin; Task 2 pins it for banned-and-deactivated accounts on every path |
| T-2zy-07 | S | Entra tid read from an id_token without signature verification | medium | mitigate | Task 3: only from the back-channel token response over TLS, aud must equal the client id, GUID format, empty otherwise; README trust note; apps pair TenantID with Provider microsoft |
| T-2zy-08 | I | new log lines | medium | mitigate | Tasks 1 and 3: log only provider, user_id and err; never id_token, access token, code or secrets (TestSettings no-secret-in-logs stays green) |
| T-2zy-09 | R | discarded user.Register / user.Created / sign-in listener errors | low | mitigate | Tasks 1 and 3: Error-level log lines with user_id; control flow unchanged |
| T-2zy-10 | D | suspension (failed-password throttle) used as a refresh refusal | medium | accept | Refresh checks bans only, so failing logins against an email cannot log its owner out |
| T-2zy-11 | E | deactivated account reactivated by its owner (password or social sign-in) | low | accept | User decision OD-1: deactivation is self-service and reversible; a ban is the lock and is refused everywhere |
| T-2zy-12 | E | a JWT minted within the same second before a revocation stays valid until it expires | low | accept | Cutoff truncated to whole seconds (JWT iat precision) so a reactivating login in that second works; refresh and login still refuse bans |
| T-2zy-SC | T | npm/pip/cargo/go installs | low | accept | No new dependency: golang-jwt/jwt/v5 (user plugin), golang.org/x/oauth2 and goth (sociallogin) are already direct requirements; cabana uses only existing code |
</threat_model>
<verification>
- User plugin (../sm-demo-app/plugins/golem15/user): GOWORK=off go vet ./... and GOWORK=off go test -count=1 ./... green with Docker after every task.
- Sociallogin (../sm-sociallogin-plugin): go vet ./... and go test -count=1 ./... green with its own go.work, and the scratch-GOWORK run against the modified user plugin green.
- summercms.go after Task 4: go vet ./..., go test -short ./..., go test ./modules/cabana/... (Docker), TestDocsTree and summer docs:build --check green; only the Task 4 paths staged.
- No push, tag, changelog post or app commit before the Task 5 decision.
</verification>
<success_criteria>
- 2zy-1: a /register body sets only name, surname, email, password, preferred_locale and marketing_consent; IP columns are server-set; admin and application flows unchanged.
- 2zy-2: Microsoft identities carry profile_data.tenant_id; SignedInEvent carries TenantID.
- 2zy-3: SignedInEvent fires on every successful social sign-in with provider and identity; register, created and sign-in listener errors are logged.
- 2zy-4: deactivation ends sessions and API tokens and is undone only by the owner signing in again (fresh token) or an administrator; a ban is refused by login, social sign-in and refresh and revokes sessions and API tokens; /refresh refuses deactivated, banned and cut-off subjects.
- 2zy-5: cabana ClearOnChange exists and is documented; adding or removing a member and changing the organisation in the Users form clear organisation_role; the known-gap test asserts the closed gap.
- Both plugin READMEs document the allow-list, the new event, the profile_data key, the refresh rule and the deactivation/ban semantics in the commits that change them; the cabana README and docs/backend/relation-manager.md document ClearOnChange in the framework commit.
</success_criteria>
<output>
Create `.planning/quick/261022-2zy-user-and-sociallogin-plugin-security-gap/261022-2zy-SUMMARY.md` when done, listing every commit hash per repo (summercms.go, sm-user-plugin, sm-sociallogin-plugin), the scratch-GOWORK cross-check result, anything skipped because another session had the files open, and the Task 5 outcome.
</output>

View File

@@ -0,0 +1,149 @@
---
phase: quick-261022-2zy
plan: 01
quick_id: 261022-2zy
subsystem: sm-user-plugin, sm-sociallogin-plugin, cabana
tags: [security, registration, offboarding, entra, events, cabana, relations]
status: complete
requires: [summercms v0.4.2 released]
provides:
- User.RegisterFillable (registration allow-list)
- classes.RevokeUserAccess; deactivate/ban revoke sessions and API tokens; /refresh subject check
- sociallogin Profile.TenantID, PendingPayload.TenantID, profile_data.tenant_id
- sociallogin classes.SignedInEvent / SignedInEventName
- cabana RelationContract.ClearOnChange and FieldRelationContract.ClearOnChange
affects: [fonoteka.go, sm-bm-app, sm-demo-app, sm-grzybyfunkcjonalne-app, sm-summercmsio-app]
tech-stack:
added: []
patterns: [explicit registration allow-list, revocation cutoff in whole seconds, declarative ClearOnChange on relation contracts]
key-files:
created:
- ../sm-demo-app/plugins/golem15/user/offboarding_test.go
- ../sm-sociallogin-plugin/classes/events.go
- ../sm-sociallogin-plugin/signin_event_test.go
- ../sm-sociallogin-plugin/tenant_test.go
- modules/cabana/clear_on_change_test.go
- modules/cabana/clear_on_change_field_test.go
modified:
- ../sm-demo-app/plugins/golem15/user/models/user.go
- ../sm-demo-app/plugins/golem15/user/controllers/registration.go
- ../sm-demo-app/plugins/golem15/user/controllers/api_controller.go
- ../sm-demo-app/plugins/golem15/user/classes/admin_actions.go
- ../sm-demo-app/plugins/golem15/user/controllers/organisations_admin_controller.go
- ../sm-demo-app/plugins/golem15/user/controllers/users_admin_controller.go
- ../sm-sociallogin-plugin/classes/flow.go
- ../sm-sociallogin-plugin/classes/providers_goth.go
- ../sm-sociallogin-plugin/classes/registration.go
- modules/cabana/relation.go
- modules/cabana/relation_field.go
- modules/cabana/crud.go
decisions:
- "Registration fills only User.RegisterFillable (name, surname, email, password, preferred_locale, marketing_consent); IP columns only from RegisterOptions.IP; Fillable() unchanged"
- "RevokeUserAccess cutoff is time.Now() truncated to whole seconds (JWT iat precision)"
- "/refresh refuses missing/deactivated, banned and pre-tokens_valid_after subjects with the existing 401 body; suspension is not checked"
- "Microsoft provider exchanges the code itself to keep the id_token; tid trusted only with aud == client id and GUID format"
- "cabana ClearOnChange is applied only on hasMany link/unlink and on a belongsTo key change in the form save; created-child commits are untouched"
metrics:
duration: ~95 min
completed: 2026-10-22
actuals:
tokens: 46000
tasks: 4
commits: 1
plan_head_before: 9563bc8
plan_head_after: 9516a2b
---
# Quick 261022-2zy: user and sociallogin plugin security gaps Summary
Registration allow-list, session and API-token revocation on deactivate/ban with a /refresh subject check, Entra tenant ID and a SignedInEvent in sociallogin, and an additive cabana `ClearOnChange` that clears a stale `organisation_role`. Tasks 1 to 4 are committed locally in three repos. Task 5 (push, tags, changelog, pointer bumps) is waiting for the user's go-ahead.
`commits`/`plan_head_*` in the frontmatter measure summercms.go only (one framework commit since 9563bc8). The plugin repos are listed below.
## Commits
### sm-user-plugin (`../sm-demo-app/plugins/golem15/user`, branch master, base 9986342 = origin/master = v0.1.6)
| Hash | Subject |
|------|---------|
| 499b6dc | test: migration rollback counts include the email_verified_at migration (deviation, see below) |
| b6b7ac7 | fix: registration fills only an explicit allow-list and never takes the IP columns from the body |
| e0b5ffe | fix: log /register listener failures instead of discarding them |
| c3712dc | fix: deactivate and ban revoke sessions and API tokens, and /refresh refuses deactivated, banned and cut-off subjects |
| aa83969 | fix: clear a stale organisation_role when a user's organisation changes in the admin (last commit; needs summercms with ClearOnChange) |
### sm-sociallogin-plugin (`../sm-sociallogin-plugin`, master, base 9fd58db = origin/master)
| Hash | Subject |
|------|---------|
| 04f4583 | test: a banned account is refused and never restored by social sign-in, and the deactivation semantics are documented |
| 5c9f846 | feat: record the Entra tenant ID of a Microsoft sign-in in profile_data |
| 4d3b2dd | feat: fire SignedInEvent after every social sign-in and log listener failures |
go.mod unchanged (still requires sm-user-plugin v0.1.5 and summercms v0.3.1).
### summercms.go (master, after v0.4.2)
| Hash | Subject |
|------|---------|
| 9516a2b | feat(cabana): ClearOnChange clears dependent columns when a relation changes a foreign key |
Nothing pushed or tagged. The sm-demo-app working tree now shows `M plugins/golem15/user`, the checkout moved to aa83969. That pointer bump is not committed; it belongs to Task 5.
## Test results
- User plugin: `GOWORK=off go vet ./...` and `GOWORK=off go test -count=1 ./...` (Docker) green before every commit, the last one included.
- Sociallogin: `go vet ./...` and `go test -count=1 ./...` green with its own go.work, which uses the published user plugin at 9986342.
- Scratch-GOWORK cross-check: sociallogin against summercms.go's working tree and the modified user plugin (aa83969), using `/tmp/claude-1000/.../scratchpad/go.work`. Green: `ok git.golem15.com/golem15/sm-sociallogin-plugin`.
- summercms.go: `go vet ./...`, `go test -count=1 -short ./...`, `go test -count=1 ./modules/cabana/...` (Docker, 82 s), `go test -run '^TestDocsTree$' ./cmd/summer` and `go run ./cmd/summer docs:build --check` all green.
## Deviations from Plan
### Auto-fixed Issues
**1. [Rule 3 - Blocking] Pre-existing migration rollback test failures in the user plugin**
- **Found during:** Task 1, the first full suite run.
- **Issue:** `updates/` TestUserGroupsMigration, TestExtendUsersMigration, TestCreateUserThrottleMigration and TestCreateJwtBlacklistMigration failed at origin/master. Their rollback counts were one short since migration 202610210001 (email_verified_at, v0.1.6) was added. This blocked "full suite green at every commit".
- **Fix:** Bumped the four rollback counts and their comments. Test-only change, committed separately.
- **Commit:** 499b6dc
**2. [Rule 2] Extra test for the /register listener-error logging**
- Added TestRegisterListenerErrors (register_test.go) to commit e0b5ffe. The plan had no test for that commit.
**3. [Rule 1] TestAdminOrganisationMembers pinned the old behaviour**
- admin_organisations_test.go expected a foreign member's role to survive unlink and link. It now expects the unlink to clear the role. Committed with aa83969.
**4. Test file layout (sociallogin)**
- The tenant tests that do not need the event live in a new `tenant_test.go` (TestTenantProfileData) inside commit 5c9f846. The plan kept event-dependent assertions out of that commit; this keeps the tenant behaviour tested in the same commit that adds it.
- `fakeprovider_test.go` was not changed: the fake provider already returns whatever `classes.Profile` it is given, so the new `TenantID` field passes through.
- To capture mails, TestInactiveAndBanned now boots with the `log` mail driver and an slog handler. A deactivated-without-ban control subtest proves the capture sees the reactivate mail.
**5. Test file layout (cabana)**
- The belongsTo cases are in an internal-package file, `clear_on_change_field_test.go` (TestClearOnChangeFieldBoot, TestClearOnChangeBelongsTo). They use a small dedicated fixture because no roster column is both nullable and fillable.
- The hasMany cases are in `clear_on_change_test.go` (TestClearOnChangeRelationBoot, TestClearOnChangeHasMany), on the df fixtures.
- The docs checker refuses Go code blocks without `src=`. So the docs example is the existing `example_form_seams_test.go` MembersController team contract, which gained `ClearOnChange: []string{"team_role"}` and a `Member.TeamRole` field; `summer docs:sync` refreshed the snippet. Its Example output is unchanged.
- Boot errors name the relation or field and the column, and `bootErr` adds the plugin and controller, as the existing contract errors do.
**6. ClearOnChange scope**
- Applied in linkRelated and unlinkRelated for hasMany, which covers deferred link and unlink because they use the same path, and in assignBelongsTo.
- Not applied when a deferred commit attaches a child created in the form session, or when CreateChild sets the key: that would wipe values the admin just entered in the child form ("no other behaviour changes").
**7. User README dependency note**
- It says the role cleanup needs "SummerCMS v0.4.3 or later". If Task 5 picks a different tag, update this line together with the go.mod require bump.
## Known Stubs
None.
## Threat Flags
None beyond the plan's threat model. The new log lines carry only provider, user_id and err; the id_token, access token and code are never logged.
## Task 5: awaiting go-ahead
Blocking checkpoint:decision. Choose publish-all, plugins-first or hold. Nothing has been pushed, tagged, posted or pointer-bumped.
## Self-Check: PASSED
- Files exist: offboarding_test.go, classes/events.go (sociallogin), signin_event_test.go, tenant_test.go, modules/cabana/clear_on_change_test.go, modules/cabana/clear_on_change_field_test.go.
- Commits exist: 499b6dc, b6b7ac7, e0b5ffe, c3712dc, aa83969 (user plugin); 04f4583, 5c9f846, 4d3b2dd (sociallogin); 9516a2b (summercms.go).

View File

@@ -0,0 +1,367 @@
---
phase: quick-261022-4z8
plan: 01
quick_id: 261022-4z8
slug: fix-the-open-19-8-re-review-findings-wr-
type: execute
mode: quick-full
wave: 1
depends_on: []
autonomous: true
requirements:
- WR-09 # lines lost through a live view after its context is cancelled (verification gap 1)
- WR-10 # signal-killed delegate prints raw argv with secrets (verification gap 2)
- WR-11 # dash-leading secret values not masked in tryArgs (verification gap 2)
- WR-12 # migrate:status --json omits the framework sets (verification gap 3)
- WR-13 # migrate exits 1 when only the re-read after a successful Migrate fails
- WR-14 # Process's plain path prints item names without Sanitize
- IN-14 # ErrReported doc comment still says exit code 1
- IN-15 # runLive builds the program before claiming the slot
- 4z8-docs # disposition record + todo (IN-10..IN-13, exit-130 suggestion deferred)
files_modified:
# Task 1: bonfire (IN-14, WR-14, IN-15, WR-09)
- modules/bonfire/errors.go
- modules/bonfire/helpers.go
- modules/bonfire/live.go
- modules/bonfire/live_test.go
- modules/bonfire/README.md
- docs/console/writing-commands.md
- scripts/check-phase19.8.sh
# Task 2: secret masking (WR-11, WR-10)
- modules/bonfire/errors.go
- modules/bonfire/errors_test.go
- internal/argmask/argmask.go
- cmd/summer/runtime.go
- cmd/summer/runtime_test.go
- docs/console/introduction.md
- docs/console/writing-commands.md
- scripts/check-phase19.8.sh
# Task 3: lagoon (WR-12, WR-13) + records
- modules/lagoon/migrations.go
- modules/lagoon/commands.go
- modules/lagoon/migrations_test.go
- modules/lagoon/example_test.go
- modules/lagoon/pending_test.go
- modules/lagoon/README.md
- docs/console/setup-and-maintenance.md
- docs/database/migrations.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
estimate:
tokens: 160000
raw_tokens: 160000
tasks: 3
confidence: low # estimate-calibration: factor 1, sample_count 0
must_haves:
truths:
- "WR-09: when the context passed to bonfire.Process is cancelled while its live view runs, the view ends with its last frame and is unregistered without waiting for the work function to return; afterwards Output.Println, Printf, Info and bonfire.PrintAbove print plainly (PrintAbove reports false) and none of those lines is lost. printAbove also reports false for a program whose Run has ended on its own."
- "WR-14: bonfire.Process's plain path (no animation: -n on a terminal, forced color off a terminal, nested in another view) prints item names through bonfire.Sanitize; the process view's operation label is sanitized too."
- "IN-15: runLive claims the single live slot before it builds the bubbletea program; a widget that loses the slot builds no program, so no child context is registered on the caller's context and no TEA_TRACE file is opened."
- "IN-14: the ErrReported doc comment says the exit code is 1 or the code given with WithExitCode."
- "WR-11: the wrong-directory Try line masks the argument after a secret-named long flag in the '--flag value' form whatever its first character (a value such as -Xk9!q becomes ***); only a following '--' is left unmasked."
- "WR-10: summer's error for an app binary killed by a signal or failing to start names the argv with the same masking as the Try line (one shared implementation in internal/argmask), so a --password, --api-token=... or dash-leading secret value never reaches stderr; the exit code for a signal death stays 1. No exported framework identifier is added, removed or changed."
- "WR-12: lagoon.Status (and so migrate:status and migrate:status --json) reads migrationSets, the list Migrate and PendingMigrations use: framework sets first, then plugins with migrations; --json lists every pending migration PendingMigrations returns, a pending framework-set migration included. Status keeps its signature and StatusRow its fields."
- "WR-13: when Migrate succeeds but the follow-up PendingMigrations read fails, migrate prints a warning, reports every migration that was pending before the run as applied, prints 'Applied N migrations' and exits 0; when both fail it still returns both errors."
- "Every code finding is its own commit with its pinning test (assertion RED recorded on the unchanged code), README/docs updated in the same commit; no Co-Authored-By trailer; nothing pushed or tagged."
- "19.8-REVIEW-DISPOSITION.md marks WR-09..WR-14, IN-14 and IN-15 fixed with their commit hashes (frontmatter and table, open: 0), notes the WR-05/WR-07/WR-08 remainders (WR-14, WR-12, WR-11), and marks IN-10..IN-13 deferred; the todo carries IN-10..IN-13 and the exit-130 suggestion."
artifacts:
- path: modules/bonfire/live.go
provides: "runLive claims the slot before tea.NewProgram, ends and unregisters the view on ctx cancellation; liveView.done; printAbove refuses a finished program"
contains: "case <-v.done:"
- path: modules/bonfire/helpers.go
provides: "Process's plain path sanitizes item names"
contains: "out.Success(Sanitize(item))"
- path: modules/bonfire/errors.go
provides: "ErrReported comment with WithExitCode; tryArgs delegating to internal/argmask"
contains: "argmask.Join"
- path: internal/argmask/argmask.go
provides: "Join (shell-quoted argv with secret flag values masked) and Quote, shared by bonfire and cmd/summer"
contains: "func Join(args []string) string"
- path: cmd/summer/runtime.go
provides: "runAppCmd names argv through argmask.Join"
contains: "argmask.Join(argv)"
- path: modules/lagoon/migrations.go
provides: "Status built from migrationSets"
contains: "func Status(gdb *gorm.DB, plugins []party.Plugin) ([]StatusRow, error)"
- path: modules/lagoon/commands.go
provides: "migrate warns instead of failing on a failed re-read after success"
contains: "Could not re-read the migration history"
- path: modules/bonfire/live_test.go
provides: "TestPrintAboveAfterCancel (WR-09), TestNestedProcessBuildsNoProgram (IN-15), plain-path cases in TestProcessViewStripsControl (WR-14)"
contains: "func TestPrintAboveAfterCancel"
- path: cmd/summer/runtime_test.go
provides: "TestRunAppCmdMasksSecrets and the masked signal subtest of TestDelegateExitCodePassthrough (WR-10)"
contains: "func TestRunAppCmdMasksSecrets"
- path: modules/lagoon/pending_test.go
provides: "TestMigrateStatusJSON pending framework migration (WR-12), TestMigrateRereadFailureWarns (WR-13)"
contains: "func TestMigrateRereadFailureWarns"
- path: .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
provides: "dispositions recorded"
contains: "open: 0"
key_links:
- from: cmd/summer/runtime.go
to: internal/argmask/argmask.go
via: "runAppCmd wraps every child error as summer: <argmask.Join(argv)>: <err>"
pattern: "argmask\\.Join\\(argv\\)"
- from: modules/bonfire/errors.go
to: internal/argmask/argmask.go
via: "tryArgs and the cd line use argmask.Join and argmask.Quote (one masking implementation)"
pattern: "argmask\\.(Join|Quote)"
- from: modules/lagoon/migrations.go
to: modules/lagoon/migrations.go
via: "Migrate, Status and PendingMigrations all call migrationSets(gdb, plugins)"
pattern: "migrationSets\\(gdb, plugins\\)"
- from: modules/bonfire/live.go
to: modules/bonfire/helpers.go
via: "Process passes its ctx to runLive; runLive's cancel watcher finishes and unregisters the view; printAbove checks liveView.done"
pattern: "case <-v\\.done"
- from: modules/bonfire/helpers.go
to: modules/bonfire/errors.go
via: "Process plain path calls Sanitize on the item"
pattern: "Sanitize\\(item\\)"
---
<objective>
Fix the open findings of the 19.8 code re-review (`19.8-REVIEW.md`): WR-09, WR-10, WR-11 and WR-12 (the three verification gaps in `19.8-VERIFICATION.md`), WR-13, WR-14, IN-14 and IN-15. Then record the dispositions and defer IN-10..IN-13 plus WR-10's exit-130 suggestion to the existing todo.
Purpose: close the three gaps that keep phase 19.8 at `gaps_found`: lines lost after a cancelled live view, secrets leaking through argv, and `migrate:status --json` missing framework sets. The four quality follow-ups go in the same pass.
Output: nine local commits on master. There is one `fix(...)`/`docs(bonfire)` commit per finding, each with its pinning test and README/docs, and then one `docs(19.8)` records commit. Nothing is pushed, tagged, released or posted to the changelog.
Shape: this is a gap-closure-style quick task. The orchestrator fixed the grouping (three tasks), so there is no tracer task. Each finding follows RED → fix → GREEN → commit, the pattern of plan 19.8-05 (`19.8-05-SUMMARY.md`, "Each review finding: pinning test red on unchanged code, fix, green, one fix(...) commit with test and docs").
**One commit per finding (hard rule).** Each task holds several findings, and each finding is its own commit, in the order the task lists. The commit subject follows the repo's style and ends with the finding ID in parentheses. There is NO `Co-Authored-By` trailer: the user's CLAUDE.md overrides any attribution reminder. Planning docs and code are never in the same commit.
Decisions (planner's discretion; the user is asleep and the run is unattended, so the executor does not revisit these and records them in the SUMMARY):
- **P-01 (WR-09 design).** The live program no longer takes the caller's context. Drop bubbletea's context program option from `tea.NewProgram` in `runLive`. Instead, `runLive` starts a watcher goroutine. When `ctx` is done, the watcher takes `liveMu`, unregisters the view if it is still the active one, sends `finishMsg{err: ctx.Err()}`, waits for `finished` and releases `liveMu`. This is the same unregister/finish/wait-under-one-lock step as the deferred cleanup. When `finished` closes first (Run ended on its own, a signal kill, or the normal end), the watcher only unregisters the view under `liveMu` if it is still active. In addition, `liveView` gets a `done <-chan struct{}` field (runLive's `finished` channel, created before the claim). `printAbove` checks it under `liveMu` after the owner check. When it is closed, `printAbove` unregisters the view and returns false. Why not the review's check-then-Send: a cancellation landing between the check and `Program.Send` would still drop the line, because bubbletea's Send returns as soon as its context is done. With P-01 the program stays alive until the watcher has unregistered it under the lock, so every line is either delivered to a running program or printed plainly. P-01 also renders the final frame with the failed rows, which the review notes the killed program never did. Spinner and Progress pass `context.Background()`, so P-01 does not change them. Fallback: if the graceful finish deadlocks or cannot be made to pass under `-race`, implement the review's version and record the deviation. That version keeps the context option, checks `ctx.Done()` and `done` in `printAbove`, and clears `activeView` when `finished` closes.
- **P-02 (IN-15).** Claim first, then build. Under `liveMu`, return `false, nil` if `activeView` is set. Otherwise build the program while still holding the lock, register the view and unlock. `NewProgram` takes no bonfire lock and calls nothing that prints, so this cannot deadlock.
- **P-03 (WR-14).** Sanitize at the call sites: `out.Success(Sanitize(item))` in `Process`'s plain path, and `Sanitize(op)` for the process view's operation label. Do not sanitize inside `console.status`: callers pass intentionally styled (ANSI) messages to Info/Success/Warning, and those must keep their styling.
- **P-04 (WR-11).** In the `--flag value` form, mask the next argument after a secret-named long flag whenever it exists and is not `--`, whatever its first character. `tryArgs` cannot see flag types, so a secret-named boolean flag over-masks the following argument. That is the safe side. The existing case `sync --token --force` therefore changes from `--token --force` to `--token ***`, and the test comment says why. pflag also takes `--force` as the token's value there (v1.0.10 flag.go, the `'--flag arg'` branch).
- **P-05 (WR-10).** The user asked for the same masking as `tryArgs` with no new exported API unless needed. It is not needed: the masking moves to a new internal package `internal/argmask`, importable from both `modules/bonfire` and `cmd/summer` (same module root). It provides `Join(args []string) string` (shell-quoted, space-separated, secret values masked, no leading space) and `Quote(s string) string`, which is bonfire's former `shellQuote`. bonfire keeps the unexported `tryArgs` as a thin wrapper (empty for no args, otherwise a space plus `argmask.Join(args)`). It calls `argmask.Quote` for the `cd` path and drops its own `secretWords`/`secretFlag`/`shellQuote`, which nothing else references (verified by grep at planning time). `runAppCmd` builds its error with `argmask.Join(argv)` in every branch: start failure, signal death, and the already-Reported exit branch. The exit code stays 1 on a signal death (plan 05's CR-02 truth). The exit-130 suggestion goes to the todo.
- **P-06 (WR-12).** `Status` iterates `migrationSets(gdb, plugins)`: framework sets first (`StatusRow.Plugin` is the set's history ID, for example `summercms.attach`, the same convention as `MigrationRef.Plugin`), then plugins with at least one migration. A plugin whose `HasMigrations` returns none no longer gets an empty row; Migrate and PendingMigrations skip it too, which keeps the three readers consistent. The signature of `Status`, the `StatusRow` type and its fields are unchanged. No consumer outside this repo calls `lagoon.Status` (grep over the meta repo at planning time). The `migrate:status` Description stays as it is, because changing it would churn the list goldens and the list excerpt in `docs/console/introduction.md`. Its Long help and the docs change. The `--json` builder in commands.go keeps its loop: it now covers framework rows, because `row.Plugin` equals `ref.Plugin` for them.
- **P-07 (WR-13).** When `migrateErr == nil` and the re-read fails, `applied` is the pending list from before the run. migrate prints `out.Warning("Could not re-read the migration history: " + bonfire.Sanitize(err.Error()))` (wording may be refined; it must contain `Could not re-read the migration history`), then the per-set lines and `Applied N migrations`, and returns nil. When both fail, it keeps `errors.Join(migrateErr, err)`.
- **P-08.** Each new top-level pinning test name joins its list in `scripts/check-phase19.8.sh`, in the same commit as the test, so the phase gate keeps requiring it. Tests extended in place are already listed.
- **P-09.** `docs/setup/upgrading.md` is not changed, because no release is cut. The SUMMARY gets a "Release-note input" section for whoever tags next: `lagoon.Status` and `migrate:status` now list framework sets; a cancelled Process view ends with its final frame; secret masking now covers dash-leading values and delegate signal errors.
</objective>
<execution_context>
@~/.claude/msd-core/workflows/execute-plan.md
@~/.claude/msd-core/templates/summary.md
</execution_context>
<context>
@CLAUDE.md
@.planning/phases/19.8-console-commands-overhaul/19.8-REVIEW.md
@.planning/phases/19.8-console-commands-overhaul/19.8-VERIFICATION.md
@.planning/phases/19.8-console-commands-overhaul/19.8-05-SUMMARY.md
@.planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
@.planning/todos/pending/2026-10-22-console-review-info-findings.md
Source, read once each, only the regions named:
@modules/bonfire/live.go (runLive, liveView, watchSignals, printAbove: about lines 323-487; processModel.View: about lines 287-320)
@modules/bonfire/helpers.go (Process: lines 140-180)
@modules/bonfire/errors.go (ErrReported: about line 73; wrongDirectory, tryArgs, shellQuote: about lines 250-340)
@modules/bonfire/output.go (emit, Println, Printf, status: lines 170-245)
@modules/bonfire/live_test.go (lockedBuffer, TestPrintAboveOrderedNoLoss, TestProcessViewStripsControl)
@modules/bonfire/errors_test.go (TestWrongDirectoryMasksSecrets, carriesInjected)
@modules/bonfire/widgets_test.go (TestTableStripsControl: the console construction with Globals{Color: ColorAlways})
@cmd/summer/runtime.go (appStreams, runAppCmd, newAppCmd, runChild: lines 130-238)
@cmd/summer/runtime_test.go (TestExitHelperProcess, exitHelperRunner, TestDelegateExitCodePassthrough: lines 290-370)
@modules/lagoon/migrations.go (Status, migrationSets, PendingMigrations)
@modules/lagoon/commands.go (migrate Run: lines 151-195; migrate:status: lines 255-295)
@modules/lagoon/pending_test.go (runLagoon, TestMigrateStatusJSON, TestMigrateAppliedLines)
@modules/lagoon/migrations_test.go (TestTwoPluginMigrationSetsIsolated: lines 138-260)
@modules/lagoon/example_test.go (migrateBlog, TestDocsMigrate: lines 335-372)
@scripts/check-phase19.8.sh (test name lists: lines 110-200)
Environment rules for every step (from the orchestrator; non-negotiable):
- **GOCACHE.** Every go command, including any script that calls go (`scripts/check-phase19.8.sh`), runs with `env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache`. Below, `$GC` stands for that literal path; write it out in full, because the Bash tool runs fish. Wrap multi-statement scripts or `VAR=$(…)` in `bash -c '…'`. The disk is nearly full: do not create other build caches and do not delete the gocache. The orchestrator deletes it at the end.
- **Docker.** Docker-backed tests run with `env GOCACHE=$GC TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test …`. If the Docker socket is denied, run the same command through `sudo -u golem` (CLAUDE.md).
- **Concurrent sessions.** Record `BASE=$(git rev-parse HEAD)` before the first edit. Before editing any file, run `git status --short -- <file>`. If it shows changes this task did not make, do not edit that file. Record the skipped update in the SUMMARY as a follow-up. Never touch, stage or commit `.planning/STATE.md`, `.planning/state.json`, `.msd/`, any cabana or `docs/backend` file, or other quick or phase directories. Commit with explicit paths only: `git add -- <paths>` then `git commit -m "<subject>" -- <paths>`. The `-- <paths>` form commits only those paths, even if another session has staged something. Never use `git add -A` or `git add .`.
- **Foreign test failures.** If `cmd/summer` docs tests (TestDocsTree and others) fail only because of another session's uncommitted files, export clean HEAD with `bash -c 'D=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/clean-head; mkdir -p "$D" && git archive HEAD | tar -x -C "$D"'`, run the failing package's tests there with the same GOCACHE, record both results in the SUMMARY, then remove that export directory.
- **Known flake.** `TestOIDCSubjectIsRandomSmoke` (wristband; deferred-items.md) can fail once at random. Rerun it once before treating it as a failure.
</context>
<tasks>
<task type="auto" tdd="true">
<name>Task 1: bonfire live views and Process: IN-14, WR-14, IN-15, WR-09 (four commits)</name>
<files>modules/bonfire/errors.go, modules/bonfire/helpers.go, modules/bonfire/live.go, modules/bonfire/live_test.go, modules/bonfire/README.md, docs/console/writing-commands.md, scripts/check-phase19.8.sh</files>
<behavior>
- IN-14: no test (doc comment only).
- WR-14 (extend TestProcessViewStripsControl): (a) the existing model case also builds the process model with operation "Push\x1b]0;pwned\x07ing", and the View carries none of the injected sequences while showing "Push" and "ing"; (b) plain path, terminal: newConsole(tty true, interactive true) plus setGlobals(Globals{Color: ColorAlways, NoInteraction: true}), so live() is false and the profile is colored; Process over the item "evil\x1b]0;pwned\x07item" with fn returning nil prints no injected sequence, and "evil" and "item" are visible; (c) plain path off a terminal: tty false plus Globals{Color: ColorAlways}, same assertions. (b) and (c) fail on the unchanged code; TestTableStripsControl shows the colored writer passes OSC and BEL through.
- IN-15 (new TestNestedProcessBuildsNoProgram): on a tty console, inside c.Spinner("outer", …), call t.Setenv("TEA_TRACE", <tempdir>/trace.log), then run Process with a test-only counting context over one item "x". The counting context is a type whose Done returns a non-nil channel that is never closed, whose Err and Value return nil, and which implements AfterFunc(f func()) (stop func() bool), counting registrations minus stops under a mutex. context.WithCancel registers a child through the parent's AfterFunc when the parent is not a stdlib cancel context. Assert: fn ran once; the trace file does not exist; outstanding registrations are 0; the "x" success line reached the buffer. On the unchanged code NewProgram opens the trace file and registers one child that is never stopped, so the test fails.
- WR-09 (new TestPrintAboveAfterCancel): use a tty console on a lockedBuffer, ctx and cancel from context.WithCancel, and items "first", "second", "third". fn records each item it gets. On "second" it calls cancel(), then polls for up to 2s (every 5ms) until activeView is nil, reading it under liveMu; if the view is still registered it reports t.Errorf("view still registered … after the cancellation"). Then it calls c.Println("after-cancel-println"), c.Info("after-cancel-info") and PrintAbove("after-cancel-printabove"). PrintAbove must report false; when it does, fn writes "after-cancel-printabove\n" to the buffer, as diary does. fn then returns nil. After Process: errors.Is(err, context.Canceled) is true; fn never got "third"; ansi.Strip(output) contains each of the three after-cancel lines exactly once; PrintAbove("after") is false. On the unchanged code the poll times out, PrintAbove reports true and the lines are lost (the review reproduced this 3 of 3 on a pty), so the test fails without a sleep-based race.
</behavior>
<action>
Work in this order. Each step is one commit. Before the first edit, record BASE (see context). For every code finding, write or extend the test first and run it on the unchanged code. Paste the failing output (the RED) into your notes for the SUMMARY. Only then fix, run it GREEN, update the docs and commit.
1. **IN-14 (doc only, commit 1).** In modules/bonfire/errors.go, change the ErrReported doc comment so it says the error block prints nothing and the exit code is 1, or the code given with WithExitCode (review IN-14 fix text). The `var ErrReported` line itself stays byte-identical. No test. Commit: `docs(bonfire): the ErrReported comment names the WithExitCode code (IN-14)`, paths modules/bonfire/errors.go.
2. **WR-14 (commit 2).** Extend TestProcessViewStripsControl in modules/bonfire/live_test.go as described in behavior (a)-(c), reusing carriesInjected from errors_test.go. It is the same package, `bonfire`. Run it RED. Fix per P-03: in modules/bonfire/helpers.go, the plain path's success line becomes out.Success(Sanitize(item)). The failure line already goes through failureText. In modules/bonfire/live.go processModel.View, sanitize the operation label (`op`) before rendering. Update modules/bonfire/README.md line 30 ("Table cells, process-view item names and errors, and failure lines are stripped…") to cover Process item names on both paths and the operation label. writing-commands.md:155 already promises this, so leave it. Commit: `fix(bonfire): strip control characters from Process's plain item lines and the view's label (WR-14)`, paths helpers.go, live.go, live_test.go, README.md.
3. **IN-15 (commit 3).** Add TestNestedProcessBuildsNoProgram to modules/bonfire/live_test.go per behavior and run it RED. Fix per P-02 in runLive: take liveMu; if activeView is set, unlock and return false, nil. Otherwise build the program with the existing options, set activeView and unlock. Keep the rest of runLive unchanged in this commit. Update runLive's doc comment: it claims the slot before building the program, so a widget that loses the slot builds no program. Add TestNestedProcessBuildsNoProgram to the bonfire list in scripts/check-phase19.8.sh (the array that ends with `TestProcessViewStripsControl TestTableStripsControl`), under a comment line `# quick 261022-4z8 (re-review fixes): IN-15, WR-09.` Commit: `fix(bonfire): claim the live view slot before building the program (IN-15)`, paths live.go, live_test.go, scripts/check-phase19.8.sh.
4. **WR-09 (commit 4).** Add TestPrintAboveAfterCancel per behavior and run it RED. It must fail on the poll and on the lost lines, not hang. Fix per P-01 in modules/bonfire/live.go:
- (a) Add `done <-chan struct{}` to liveView, with a field comment. Create `finished` before the claim and set it on the view.
- (b) Remove bubbletea's context program option from the NewProgram call, so the program's lifetime is the view's.
- (c) After `go prog.Run()`, start the watcher goroutine described in P-01. Select on ctx.Done() and finished. The ctx branch unregisters, sends finishMsg{err: ctx.Err()} and waits for finished, all while holding liveMu. The finished branch only unregisters under liveMu. Both branches clear activeView only when it is still this view. A nil ctx.Done() (Background) just waits for finished.
- (d) In printAbove, read activeView into a local named `v`, as in the review's sketch. After the owner check, add a non-blocking select with a `case <-v.done:` branch. When `done` is closed, set activeView to nil and return false.
- (e) Update the doc comments of runLive, liveView and printAbove. Say that cancelling ctx ends the view with its last frame and unregisters it without waiting for work, so lines printed after the cancellation print plainly below it. Say also that a program whose Run ended on its own is never sent lines.
Check that the deferred cleanup still works when the watcher has already finished the program. Its Send returns at once (Run's deferred cancel) and finished is closed. Keep watchSignals as it is.
Run TestPrintAboveAfterCancel, TestNestedProcessBuildsNoProgram, TestPrintAboveOrderedNoLoss, TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters, TestLiveViewRestoresTerminalOnSignal, TestPrintAbove and TestLiveModels with -race -count=3. All must pass.
Docs: in modules/bonfire/README.md line 30 and docs/console/writing-commands.md line 77, add one sentence: when the context given to bonfire.Process is cancelled, the view ends with its last frame and later lines print plainly below it. Add TestPrintAboveAfterCancel to the same gate list line as IN-15.
Commit: `fix(bonfire): end a live view when its context is cancelled and print later lines plainly (WR-09)`, paths live.go, live_test.go, README.md, docs/console/writing-commands.md, scripts/check-phase19.8.sh.
After each commit, run `env GOCACHE=$GC go vet ./modules/bonfire/` and `env GOCACHE=$GC go test -short ./modules/bonfire/ ./modules/diary/`. diary routes console records through bonfire.PrintAbove.
</action>
<verify>
<automated>env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go test -count=3 -race ./modules/bonfire/ -run 'TestPrintAboveAfterCancel|TestNestedProcessBuildsNoProgram|TestProcessViewStripsControl|TestPrintAboveOrderedNoLoss|TestPrintlnStaysWithItsConsole|TestLiveViewSingleOwner|TestLiveViewPanicUnregisters|TestLiveViewRestoresTerminalOnSignal|TestPrintAbove$|TestLiveModels' && env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go test -short -count=1 ./modules/bonfire/ ./modules/diary/ && env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go vet ./modules/bonfire/ && grep -v '^\s*//' modules/bonfire/live.go | grep -c 'WithContext(' | grep -qx 0 && git log --format=%s -4 > /tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/t1-subjects && grep -c '(IN-14)\|(WR-14)\|(IN-15)\|(WR-09)' /tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/t1-subjects | grep -qx 4</automated>
</verify>
<done>Four commits exist, in the order IN-14, WR-14, IN-15, WR-09, each touching only its listed paths and none with a Co-Authored-By trailer. The RED output of WR-14, IN-15 and WR-09 on unchanged code is captured for the SUMMARY. The bonfire tests pass with -race -count=3. TestPrintAboveAfterCancel and TestNestedProcessBuildsNoProgram are in the gate's bonfire list. The README and writing-commands.md describe the cancel behaviour and the sanitized plain path.</done>
</task>
<task type="auto" tdd="true">
<name>Task 2: secret masking: WR-11, WR-10 (two commits)</name>
<files>modules/bonfire/errors.go, modules/bonfire/errors_test.go, internal/argmask/argmask.go, cmd/summer/runtime.go, cmd/summer/runtime_test.go, docs/console/introduction.md, docs/console/writing-commands.md, scripts/check-phase19.8.sh</files>
<behavior>
- WR-11 (extend TestWrongDirectoryMasksSecrets): new cases. `admin:create --email a@b.c --password -Xk9!q` gives `cd ..; {bin} admin:create --email a@b.c --password ***`. `sync --client-secret -s3cr3t --verbose` gives `cd ..; {bin} sync --client-secret *** --verbose`. `sync --token -- x` gives `cd ..; {bin} sync --token -- x`. The existing case `sync --token --force` now expects `cd ..; {bin} sync --token ***` (P-04; the test comment says pflag takes `--force` as the value). Add "-Xk9" and "-s3cr3t" to the list of secrets that must not appear. The new dash cases fail on the unchanged code.
- WR-10 (new TestRunAppCmdMasksSecrets; skipped on Windows like its neighbours). argv is `admin:create --email a@b.c --password hunter2 --api-token=t0k3n --client-secret -Xk9q`. Subtest "killed by a signal": newAppCmd(ctx, os.Args[0], ".", []string{"-test.run=^TestExitHelperProcess$"}, io.Discard, io.Discard) with SUMMER_EXIT_HELPER=sigkill appended to cmd.Env, then runAppCmd(cmd, argv). The error is not ErrReported, contains "signal: killed", "--password ***", "--api-token=***" and "--client-secret ***", and contains none of hunter2, t0k3n, -Xk9q. Subtest "start failure": newAppCmd with a binary path inside t.TempDir() that does not exist; same masking assertions. Extend TestDelegateExitCodePassthrough: give its run helper an args parameter, keep the two existing subtests unchanged, and add a subtest "killed by a signal masks secrets". It runs `migrate --password hunter2` through bonfire.Execute with the sigkill helper and asserts exit 1 (bonfire.ExitFailure; P-05 keeps it), exactly one " ERROR " block, "--password ***" in stderr, and no "hunter2" anywhere. All new assertions fail on the unchanged code (raw join of argv).
</behavior>
<action>
Do WR-11 first, then WR-10. Each is its own commit.
1. **WR-11 (commit 5).** Extend TestWrongDirectoryMasksSecrets per behavior and run it RED. Fix per P-04 in tryArgs (modules/bonfire/errors.go): in the `--flag value` branch, mask args[i+1] when it exists and is not exactly `--`, and drop the leading-dash exclusion. Update tryArgs's doc comment: the next argument is masked whatever its first character, except `--`; a secret-named boolean flag over-masks its follower, which is the safe side. docs/console/writing-commands.md line 205 already promises masking. Add "(also a value that starts with `-`)" after `--password ***` there, so the docs state the rule. Commit: `fix(bonfire): mask secret flag values that start with a dash (WR-11)`, paths errors.go, errors_test.go, docs/console/writing-commands.md.
2. **WR-10 (commit 6).** Add TestRunAppCmdMasksSecrets and the TestDelegateExitCodePassthrough subtest per behavior, and run them RED. Fix per P-05:
- (a) Create internal/argmask/argmask.go with a package doc comment ("Package argmask renders a command line for messages with the values of secret-named long flags masked; bonfire's wrong-directory hint and summer's delegate errors share it"). Move bonfire's secret-word list, the secret-flag check, the masking loop (as `Join`, which joins with single spaces and has no leading space) and the shell quoting (as `Quote`) into it unchanged, apart from the WR-11 rule now in place. It imports only the standard library.
- (b) In modules/bonfire/errors.go, tryArgs returns "" for no args and otherwise a space followed by argmask.Join(args). The `cd` path uses argmask.Quote. Delete bonfire's now-unused secretWords, secretFlag and shellQuote, after grepping the package (tests included) to confirm nothing else uses them. TestWrongDirectoryMasksSecrets and TestWrongDirectoryHint must pass unchanged.
- (c) In cmd/summer/runtime.go runAppCmd, compute `args := argmask.Join(argv)` once and build the wrapped error from it in place of the raw join. The `strings` import may become unused; remove it if so. Update runAppCmd's doc comment: the error names the command with secret-looking flag values masked; a binary ended by a signal keeps exit code 1.
- (d) Add TestRunAppCmdMasksSecrets to the SUMMER list in scripts/check-phase19.8.sh, after the line holding TestDelegateExitCodePassthrough, with a comment `# quick 261022-4z8 (re-review fixes): WR-10.`
- (e) Docs: in docs/console/introduction.md line 146, extend the sentence about a binary killed by a signal. The block names the command with the values of secret-looking flags masked as `***`. Keep exit code `1` in the text.
Run the API check (see verification) to confirm no exported identifier changed. Commit: `fix(summer): mask secret flag values in a delegate's error (WR-10)`, paths internal/argmask/argmask.go, modules/bonfire/errors.go, cmd/summer/runtime.go, cmd/summer/runtime_test.go, docs/console/introduction.md, scripts/check-phase19.8.sh.
After each commit, run `env GOCACHE=$GC go vet ./internal/argmask/ ./modules/bonfire/ ./cmd/summer/` and the verify command below.
</action>
<verify>
<automated>env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go test -count=1 ./modules/bonfire/ -run 'TestWrongDirectory' && env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go test -count=1 ./cmd/summer/ -run 'TestDelegateExitCodePassthrough|TestRunAppCmdMasksSecrets|TestDelegateStreams|TestAppStreams|TestRunChildForwardsSignals' && env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache go vet ./internal/argmask/ ./modules/bonfire/ ./cmd/summer/ && grep -c 'strings.Join(argv' cmd/summer/runtime.go | grep -qx 0 && grep -q 'argmask.Join(argv)' cmd/summer/runtime.go</automated>
</verify>
<done>Two commits exist, WR-11 then WR-10, with no trailers. RED output is captured for both. internal/argmask is the only masking implementation, used by bonfire's Try line and by runAppCmd. A signal-killed or unstartable delegate's error never contains a secret value, and a signal death still exits 1. The API check shows no exported identifier removed or changed and none added under modules/. introduction.md and writing-commands.md describe the masking.</done>
</task>
<task type="auto" tdd="true">
<name>Task 3: lagoon WR-12 and WR-13 (two commits), then the records commit</name>
<files>modules/lagoon/migrations.go, modules/lagoon/commands.go, modules/lagoon/migrations_test.go, modules/lagoon/example_test.go, modules/lagoon/pending_test.go, modules/lagoon/README.md, docs/console/setup-and-maintenance.md, docs/database/migrations.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</files>
<precondition>Docker is reachable for testcontainers: `docker info` succeeds, or `sudo -u golem docker info` does. Otherwise stop: WR-12/WR-13 tests are Docker-only.</precondition>
<behavior>
- WR-12 (extend TestMigrateStatusJSON, Docker). After Migrate(gdb, [alpha]), delete the newest attach history row: the ID of the last entry of attach.Migrations, from the table HistoryTableName("summercms.attach"). That migration is now pending. Compute pending := PendingMigrations(gdb, plugins). For both arg variants, check three things. (1) The entries with applied false, in order, equal pending (plugin and id), each with Table equal to HistoryTableName(ref.Plugin). (2) An entry {summercms.attach, <deleted id>, applied false} is present. (3) The entries whose plugin starts with "demo." equal the old want list exactly. It fails on the unchanged code because the framework entry is missing.
- WR-12 existing tests updated to the new Status contract (not RED tests). TestTwoPluginMigrationSetsIsolated (migrations_test.go) asserts the framework rows come first in migrationSets order (summercms.attach, summercms.cabana, DeferredHistoryID, QueueHistoryID). It then checks the demo.alpha and demo.beta rows by Plugin, not by index 0 and 1, with all existing ID assertions kept. TestDocsMigrate (example_test.go, package lagoon_test) asserts that the last line is the existing acme.blog want line and the lines before it start with lagoon's framework set IDs. Leave the docs region code between `docs:start migrate` and `docs:end migrate` byte-identical, so docs/database/migrations.md needs no `summer docs:sync`.
- WR-13 (new TestMigrateRereadFailureWarns, Docker, dedicatedDB "lagoon_reread_warns"). Use plugin demo.rereadfirst, whose one migration creates a table, then plugin demo.rereadlast, whose one migration runs `ALTER TABLE <HistoryTableName("demo.rereadfirst")> RENAME COLUMN id TO id_gone`. Plugin IDs allow only lowercase letters, digits and dots. Compute before := PendingMigrations on the fresh DB. Run runLagoon(t, "local", dsn, plugins, "migrate"). Expect exit 0, stdout containing "Could not re-read the migration history", "demo.rereadfirst: 1 applied", "demo.rereadlast: 1 applied" and fmt.Sprintf("Applied %d migrations", len(before)). On the unchanged code the run exits 1 and prints no applied lines.
</behavior>
<action>
1. **WR-12 (commit 7).** Extend TestMigrateStatusJSON per behavior and run it RED with Docker. Fix per P-06 in modules/lagoon/migrations.go: Status calls migrationSets(gdb, plugins) and builds one StatusRow per set (Plugin = set id, Table = HistoryTableName(set id); IDs from the history table ordered by id, or none when the table is missing, exactly as today). The error label becomes `lagoon: status <set id>`. Update the doc comments of StatusRow ("one migration set's recorded IDs: Plugin is the plugin ID or the framework set's history ID") and Status (it reads the history of every set Migrate applies, framework sets first). In modules/lagoon/commands.go, update the migrate:status Long text: it lists the framework's own sets and each plugin with migrations, and --json prints every declared migration of those sets. Keep the Description and Examples. Keep the JSON loop; it is now correct, see P-06.
Update the existing tests per behavior: TestTwoPluginMigrationSetsIsolated and TestDocsMigrate. Grep `Status(` across modules/lagoon tests for any other caller.
Docs:
- modules/lagoon/README.md line 138 (`lagoon.Status` row): it lists the applied IDs of every set Migrate applies, framework sets first, then plugins.
- modules/lagoon/README.md line 261 and docs/console/setup-and-maintenance.md line 17: the table covers the framework sets and every plugin with migrations; --json prints every declared migration of those sets.
- docs/database/migrations.md line 84: migrate:status prints the history table and applied IDs of the framework's sets and of each plugin.
Commit: `fix(lagoon): migrate:status reads the sets migrate applies (WR-12)`, paths migrations.go, commands.go, migrations_test.go, example_test.go, pending_test.go, README.md, docs/console/setup-and-maintenance.md, docs/database/migrations.md.
2. **WR-13 (commit 8).** Add TestMigrateRereadFailureWarns to modules/lagoon/pending_test.go per behavior and run it RED with Docker. Fix per P-07 in the migrate Run of modules/lagoon/commands.go. After the follow-up PendingMigrations, the applied list starts as the pre-run pending list. On a nil re-read error it becomes appliedSince(pending, after). On a re-read error with migrateErr nil, print the warning, which includes the sanitized error, and continue. When both fail, return both errors joined. The per-set lines, the migrateErr return and the success line keep their current order.
Add a comment explaining why a failed read after a successful Migrate must not fail the run: the schema change is in place, and a deploy would roll back or alert. In modules/lagoon/README.md and docs/console/setup-and-maintenance.md (the migrate row, line 15), add one clause: if the history cannot be re-read after a successful run, migrate warns and counts every pending migration as applied. Add TestMigrateRereadFailureWarns to DOCKER_LAGOON_TESTS in scripts/check-phase19.8.sh, under `# quick 261022-4z8 (re-review fixes): WR-13.`
Commit: `fix(lagoon): warn instead of failing when the re-read after a successful migrate fails (WR-13)`, paths commands.go, pending_test.go, README.md, docs/console/setup-and-maintenance.md, scripts/check-phase19.8.sh.
3. **Full verification** (before the records commit). Run the commands in the plan-level verification section: vet, the full Docker test run, the hello workspace, the gate's self-test, go and docker stages, the API check and gofmt on the touched Go files. Fix any failure in the commit of the finding it belongs to; amend only if that commit is still the newest, otherwise add a follow-up `fix(...)` commit and record it. Apply the foreign-failure rule from the context section if it applies.
4. **Records commit (commit 9, docs only).** Edit .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md:
- In the frontmatter `findings`, set disposition `fixed` for WR-09, WR-10, WR-11, WR-12, WR-13, WR-14, IN-14 and IN-15, and `deferred` for IN-10, IN-11, IN-12 and IN-13. Set `open: 0` and keep `total: 32`.
- In the table, set the same dispositions. Each fixed row's Source cell is `fix <short hash> (quick 261022-4z8)`, matching the existing `fix b077dc3 (19.8-05)` style; IN-14's says `docs <hash> (quick 261022-4z8)`. The WR-10 cell adds `; the exit-130 suggestion is deferred to todo .planning/todos/pending/2026-10-22-console-review-info-findings.md`. IN-10..IN-13 cells read `todo .planning/todos/pending/2026-10-22-console-review-info-findings.md`.
- Keep the WR-05, WR-07 and WR-08 dispositions `fixed` and append to their Source cells `; remainder WR-14 (fix <hash>)`, `; remainder WR-12 (fix <hash>)` and `; remainder WR-11 (fix <hash>)` respectively. Leave every other row unchanged.
Edit .planning/todos/pending/2026-10-22-console-review-info-findings.md:
- Append sections `## IN-10`, `## IN-11`, `## IN-12` and `## IN-13`, each with File, Problem and Suggested fix taken from 19.8-REVIEW.md (locate code by symbol, as the existing sections do).
- Append a section `## WR-10 follow-up: exit code 128+n for a delegate killed by a signal`. It records that plan 05 specified exit 1, which this quick task kept, and gives the review's suggested mapping: SIGINT/SIGTERM as already reported with 128+n, other signals 128+n with the masked block. It also notes that docs/console/introduction.md documents exit 1 and must change with it.
- Update the frontmatter: title "Console review info findings (IN-01..IN-13) and the delegate signal exit code"; add modules/bonfire/live.go, modules/bonfire/output.go and cmd/summer/runtime.go to `files`; leave `g15office: pending`.
- Extend the Context paragraph with one sentence naming the re-review and this quick task.
Commit: `docs(19.8): re-review fixes recorded, IN-10..IN-13 and the exit-130 suggestion deferred`, paths the two files only.
5. Write the SUMMARY (261022-4z8-SUMMARY.md in this quick directory). Model it on 19.8-05-SUMMARY.md and include: plan_head_before (BASE) and plan_head_after; a commits-per-finding table; the RED output per finding; the verification table; the API check; decisions P-01..P-09 as applied, with any deviation; the release-note input (P-09); skipped doc updates, if any; and a suggested g15office comment. Do not commit the SUMMARY or PLAN and do not touch STATE.md; the orchestrator does that. Do not delete the gocache; the orchestrator removes it at the end.
</action>
<verify>
<automated>env GOCACHE=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./modules/lagoon/ -run 'TestMigrateStatusJSON|TestMigrateRereadFailureWarns|TestMigrateAppliedLines|TestPendingMigrations|TestTwoPluginMigrationSetsIsolated|TestDocsMigrate|TestMigratePretend' && grep -v '^\s*//' modules/lagoon/migrations.go | grep -c 'migrationSets(gdb, plugins)' | grep -qx 3 && grep -q '^open: 0$' .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md && grep -q '^## IN-13' .planning/todos/pending/2026-10-22-console-review-info-findings.md</automated>
</verify>
<done>Commits 7 (WR-12), 8 (WR-13) and 9 (records) exist, with no trailers and only their listed paths. The Docker-backed lagoon tests pass, with RED output captured for WR-12 and WR-13. migrate:status --json lists pending framework migrations. A failed re-read after a successful migrate exits 0 with a warning. The disposition shows open: 0 with hashes in the Source cells. The todo carries IN-10..IN-13 and the exit-130 follow-up. The full verification passes. The SUMMARY is written and not committed.</done>
</task>
</tasks>
<threat_model>
## Trust Boundaries
| Boundary | Description |
|----------|-------------|
| command line → stderr/CI logs | argv typed by an operator (passwords, tokens) is echoed in hints and errors that CI and log collectors capture |
| untrusted text → terminal | item names (remote file names, admin-typed titles, slugs) reach the terminal through Process lines |
| database → deploy pipeline | migrate's exit code and applied lines drive deploy gates and rollbacks |
## STRIDE Threat Register
| Threat ID | Category | Component | Severity | Disposition | Mitigation Plan |
|-----------|----------|-----------|----------|-------------|-----------------|
| T-4z8-01 | Information disclosure | modules/bonfire/errors.go tryArgs (WR-11) | high | mitigate | Mask the argument after a secret-named long flag whatever its first character, except `--`; pinned by the dash cases in TestWrongDirectoryMasksSecrets |
| T-4z8-02 | Information disclosure | cmd/summer/runtime.go runAppCmd (WR-10) | high | mitigate | Error built from internal/argmask.Join(argv) on every branch; TestRunAppCmdMasksSecrets (signal death, start failure) and the masked TestDelegateExitCodePassthrough subtest |
| T-4z8-03 | Tampering | modules/bonfire/helpers.go Process plain path, processModel operation label (WR-14) | medium | mitigate | Sanitize(item) and Sanitize(op); plain-path cases in TestProcessViewStripsControl |
| T-4z8-04 | Repudiation | lagoon migrate:status --json (WR-12) | medium | mitigate | Status reads migrationSets, so a deploy gate on "applied": false sees pending framework migrations; TestMigrateStatusJSON |
| T-4z8-05 | Denial of service (ops) | lagoon migrate re-read (WR-13) | medium | mitigate | A successful Migrate never exits 1 because of a failed read; warning printed; TestMigrateRereadFailureWarns |
| T-4z8-06 | Repudiation | bonfire live views (WR-09) | low | mitigate | Lines after a cancellation print plainly instead of being dropped while reported as printed; TestPrintAboveAfterCancel |
| T-4z8-07 | Denial of service | runLive on the losing path (IN-15) | low | mitigate | No program, child context or TEA_TRACE fd created when the slot is taken; TestNestedProcessBuildsNoProgram |
| T-4z8-08 | Information disclosure | IN-08 C1 bytes through Sanitize (deferred earlier) | low | accept | Still deferred in the todo; out of this task's scope |
| T-4z8-SC | Tampering | npm/pip/cargo installs | high | accept | No package installs and no new Go module dependency in this plan (internal/argmask is stdlib-only) |
</threat_model>
<verification>
Run after the WR-13 commit, before the records commit. Write `$GC` out in full; it is the gocache path from the context section.
1. `env GOCACHE=$GC go vet ./...`, which must pass.
2. `env GOCACHE=$GC TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...`, which must pass. Rerun the wristband flake once; apply the foreign-failure rule to cmd/summer docs tests.
3. `env GOCACHE=$GC go test -count=3 -race ./modules/bonfire/ ./modules/diary/`, which must pass.
4. `env GOCACHE=$GC go -C examples/hello test -count=1 ./...` (the separate workspace module), which must pass.
5. `env GOCACHE=$GC bash scripts/check-phase19.8.sh self-test`, then `env GOCACHE=$GC bash scripts/check-phase19.8.sh go`, `env GOCACHE=$GC bash scripts/check-phase19.8.sh docs` and `env GOCACHE=$GC TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 bash scripts/check-phase19.8.sh docker`, which must pass. Do not run the plugins, apps or landing stages; they touch sibling repos.
6. API check (no exported break). Replace BASE with the recorded hash and run this. S is the session scratchpad, so the repo gets no stray files:
`bash -c 'S=/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad; d() { git diff BASE..HEAD -U0 -- "$@" ":(exclude)*_test.go"; }; X="^(func (\([^)]*\) )?[A-Z]|type [A-Z]|var [A-Z]|const [A-Z])"; d modules cmd internal | grep -E "^-(func|type|var|const) " | sed "s/^-//" | sort > $S/R; d modules cmd internal | grep -E "^\+(func|type|var|const) " | sed "s/^+//" | sort > $S/A; d modules | grep -E "^\+(func|type|var|const) " | sed "s/^+//" | sort > $S/AM; echo removed-or-changed:; comm -23 $S/R $S/A | grep -E "$X"; echo added-under-modules:; comm -13 $S/R $S/AM | grep -E "$X"; rm -f $S/R $S/A $S/AM'`
Both lists must be empty. internal/argmask's exported Join and Quote live in an internal package, so they are not framework API and do not count.
7. `gofmt -l` on every Go file this task touched must print nothing.
8. Write `git log --format='%s%n%b' BASE..HEAD` to a scratchpad file, check that git exited 0, then `grep -ci 'co-authored-by'` on that file must print 0. `git log --format=%s BASE..HEAD` must show eight finding subjects ending in (IN-14), (WR-14), (IN-15), (WR-09), (WR-11), (WR-10), (WR-12) and (WR-13), plus the docs(19.8) records commit.
9. `git status --short` must show no change of ours left unstaged and no staged foreign file; foreign dirty files (STATE.md, state.json, .msd/, other sessions' edits) are left as they were.
</verification>
<success_criteria>
- All three verification gaps of 19.8 are closed by code with pinning tests: gap 1 by WR-09, gap 2 by WR-11 and WR-10, gap 3 by WR-12. WR-13, WR-14, IN-14 and IN-15 are fixed too.
- Nine local commits: eight findings, each with its test and docs, and one records commit. No trailers, no push, no tag, no changelog post.
- `go vet ./...` and `go test ./...` (with Docker) pass, as do the bonfire and diary race runs, the hello workspace, and the gate's self-test, go, docs and docker stages.
- No exported framework identifier is removed, added or changed. `lagoon.Status` keeps its signature and now returns the framework sets first.
- The disposition record shows open: 0. IN-10..IN-13 and the exit-130 suggestion are in the todo.
</success_criteria>
<output>
Write `.planning/quick/261022-4z8-fix-the-open-19-8-re-review-findings-wr-/261022-4z8-SUMMARY.md` when done (not committed by the executor).
For the orchestrator after execution:
- Commit PLAN and SUMMARY per the quick workflow, in a separate docs commit. Do not touch `.planning/STATE.md` or `state.json`; the user forbade it for this run.
- Delete the gocache directory `/tmp/claude-1000/-media-nvme-dev-golem15-summercms-io-summercms-summercms-go/e016575e-9db1-44e1-a366-55d9ae1a9f8b/scratchpad/gocache` after the last go command (executor and any verifier).
- Phase 19.8 can now be re-verified to clear its `gaps_found` status. Releasing the fixes (v0.4.x) needs the user's go-ahead.
</output>

View File

@@ -0,0 +1,258 @@
---
phase: quick-261022-4z8
plan: 01
quick_id: 261022-4z8
subsystem: console
tags: [review-fixes, bonfire, bubbletea, live-views, secrets, summer, delegates, lagoon, migrations, gate]
status: complete
gap_closure: true
requires:
- phase: 19.8-05
provides: the review fixes released as v0.4.2 and their re-review (19.8-REVIEW.md, 19.8-VERIFICATION.md gaps 1-3)
provides:
- "WR-09..WR-14, IN-14 and IN-15 fixed, one commit each, each code fix with a pinning test proven red on the unchanged code"
- "internal/argmask: one secret-masking implementation shared by bonfire's Try line and summer's delegate errors"
- "lagoon.Status / migrate:status list the framework sets first (same list as Migrate and PendingMigrations)"
- "scripts/check-phase19.8.sh requires the 4 new pinning tests by name"
- "19.8-REVIEW-DISPOSITION.md at open: 0; IN-10..IN-13 and the exit-130 suggestion in the console review todo"
affects: [phase 19.8 re-verification (gaps_found), next v0.4.x release, bonfire live views, summer delegates, lagoon migrate]
actuals:
tokens: 12206 # chars/4 over the added and removed lines of this task's diff (48825 chars)
tasks: 3
commits: 9 # MEASURED: git rev-list --count plan_head_before..plan_head_after
plan_head_before: a28957f7d0bc6dfe17b6b79fd7b5aada3d222546
plan_head_after: a48afbd11e648ba7024c687a18cce3bede24bf51
tech-stack:
added: []
patterns:
- "Live view lifetime decoupled from the caller's context: a watcher ends and unregisters the view under liveMu on ctx cancellation"
- "Claim the single live slot before building the bubbletea program"
- "printAbove refuses a view whose Run has ended (liveView.done)"
- "Framework-internal helper package (internal/argmask) shared by a module and the summer CLI instead of a new exported API"
- "Counting context (AfterFunc) to observe child-context registrations in a test"
key-files:
created:
- internal/argmask/argmask.go
modified:
- modules/bonfire/errors.go
- modules/bonfire/helpers.go
- modules/bonfire/live.go
- modules/bonfire/live_test.go
- modules/bonfire/errors_test.go
- modules/bonfire/README.md
- cmd/summer/runtime.go
- cmd/summer/runtime_test.go
- modules/lagoon/migrations.go
- modules/lagoon/commands.go
- modules/lagoon/migrations_test.go
- modules/lagoon/example_test.go
- modules/lagoon/pending_test.go
- modules/lagoon/README.md
- docs/console/writing-commands.md
- docs/console/introduction.md
- docs/console/setup-and-maintenance.md
- docs/database/migrations.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
key-decisions:
- "P-01 applied as planned: the live program no longer takes the caller's context; watchContext ends the view with its last frame and unregisters it under liveMu; printAbove checks liveView.done. The review's fallback (check-then-Send) was not needed: the design passes -race -count=3."
- "P-05 applied: the masking moved to internal/argmask (Join, Quote); no exported framework identifier added, removed or changed."
- "P-06 applied: lagoon.Status iterates migrationSets; a plugin whose HasMigrations returns none no longer gets an empty row."
- "P-07 applied: a failed re-read after a successful Migrate warns and exits 0, counting the pre-run pending list as applied."
metrics:
duration: "~20 min"
completed: 2026-10-22
---
# Quick 261022-4z8: the open 19.8 re-review findings Summary
**Cancelled live views now end with their last frame and print later lines plainly, delegate errors and Try lines mask every secret flag value (dash-leading included) through one shared `internal/argmask`, and `migrate:status` lists the framework sets that `migrate` applies. Eight review findings were fixed in eight commits, plus one records commit.**
This closes the three verification gaps of phase 19.8: gap 1 by WR-09, gap 2 by WR-11 and WR-10, gap 3 by WR-12. WR-13, WR-14, IN-14 and IN-15 are fixed as well. IN-10..IN-13 and WR-10's exit-130 suggestion are deferred to the existing todo.
## Commits per finding
| # | Finding | Commit | Subject | Pinning test |
|---|---------|--------|---------|--------------|
| 1 | IN-14 | 7c15095 | docs(bonfire): the ErrReported comment names the WithExitCode code (IN-14) | none (doc comment only) |
| 2 | WR-14 | 48e13f9 | fix(bonfire): strip control characters from Process's plain item lines and the view's label (WR-14) | TestProcessViewStripsControl (extended: operation label, two plain-path subtests) |
| 3 | IN-15 | 1ac75ad | fix(bonfire): claim the live view slot before building the program (IN-15) | TestNestedProcessBuildsNoProgram (new) |
| 4 | WR-09 | fce232d | fix(bonfire): end a live view when its context is cancelled and print later lines plainly (WR-09) | TestPrintAboveAfterCancel (new) |
| 5 | WR-11 | 707372b | fix(bonfire): mask secret flag values that start with a dash (WR-11) | TestWrongDirectoryMasksSecrets (extended) |
| 6 | WR-10 | 17c722d | fix(summer): mask secret flag values in a delegate's error (WR-10) | TestRunAppCmdMasksSecrets (new), TestDelegateExitCodePassthrough (new subtest) |
| 7 | WR-12 | 57a849d | fix(lagoon): migrate:status reads the sets migrate applies (WR-12) | TestMigrateStatusJSON (extended); TestTwoPluginMigrationSetsIsolated and TestDocsMigrate updated to the new contract |
| 8 | WR-13 | 0691d26 | fix(lagoon): warn instead of failing when the re-read after a successful migrate fails (WR-13) | TestMigrateRereadFailureWarns (new, Docker) |
| 9 | records | a48afbd | docs(19.8): re-review fixes recorded, IN-10..IN-13 and the exit-130 suggestion deferred | n/a |
No commit carries a Co-Authored-By trailer (`grep -ci co-authored-by` over `git log --format='%s%n%b' BASE..HEAD` prints 0). Nothing was pushed or tagged, and nothing was posted to the changelog.
## RED output on the unchanged code
**WR-14** (TestProcessViewStripsControl):
```
--- FAIL: TestProcessViewStripsControl (0.00s)
live_test.go:546: process view carries "\x1b]0;pwned\a": "... Push\x1b]0;pwned\aing 1/2 ..."
--- FAIL: TestProcessViewStripsControl/terminal_with_-n (0.00s)
live_test.go:577: plain Process line carries "\x1b]0;pwned\a": "\x1b[32m✓\x1b[m evil\x1b]0;pwned\aitem\n"
--- FAIL: TestProcessViewStripsControl/no_terminal,_color_forced (0.00s)
live_test.go:577: plain Process line carries "\x1b]0;pwned\a": "\x1b[32m✓\x1b[m evil\x1b]0;pwned\aitem\n"
```
**IN-15** (TestNestedProcessBuildsNoProgram):
```
--- FAIL: TestNestedProcessBuildsNoProgram (0.00s)
live_test.go:649: the nested Process built a program: the TEA_TRACE file exists
live_test.go:652: 1 child contexts still registered on the caller's context, want 0
```
**WR-09** (TestPrintAboveAfterCancel). It fails on the 2s poll, not by hanging:
```
--- FAIL: TestPrintAboveAfterCancel (2.00s)
live_test.go:686: view still registered 2s after the cancellation
live_test.go:691: PrintAbove reported true after the cancellation
live_test.go:708: "after-cancel-println" printed 0 times, want 1:
live_test.go:708: "after-cancel-info" printed 0 times, want 1:
live_test.go:708: "after-cancel-printabove" printed 0 times, want 1:
```
**WR-11** (TestWrongDirectoryMasksSecrets):
```
["sync" "--token" "--force"]: Try = "cd ..; {bin} sync --token --force", want "cd ..; {bin} sync --token ***"
["admin:create" "--email" "a@b.c" "--password" "-Xk9!q"]: Try = "... --password '-Xk9!q'", want "... --password ***"
["admin:create" ... "-Xk9!q"]: Try line repeats "-Xk9"
["sync" "--client-secret" "-s3cr3t" "--verbose"]: Try = "... --client-secret -s3cr3t --verbose", want "... --client-secret *** --verbose"
```
**WR-10** (TestDelegateExitCodePassthrough, TestRunAppCmdMasksSecrets):
```
--- FAIL: TestDelegateExitCodePassthrough/killed_by_a_signal_masks_secrets
want one error block with the password masked, got 1:
stderr "\n ERROR summer: migrate --password hunter2: signal: killed\n\n Run with -v for details.\n\n"
the password reached the output
--- FAIL: TestRunAppCmdMasksSecrets/killed_by_a_signal
error lacks "--password ***": summer: admin:create --email a@b.c --password hunter2 --api-token=t0k3n --client-secret -Xk9q: signal: killed
error repeats "hunter2" / "t0k3n" / "-Xk9q"
--- FAIL: TestRunAppCmdMasksSecrets/start_failure
error lacks "--password ***": summer: admin:create ... --password hunter2 ...: fork/exec .../missing: no such file or directory
```
**WR-12** (TestMigrateStatusJSON, Docker):
```
--- FAIL: TestMigrateStatusJSON (0.38s)
pending_test.go:318: [migrate:status --json]: pending entries = [{demo.beta ... 202610210001_create_pending_beta} {demo.beta ... 202610210002_seed_pending_beta}],
want PendingMigrations' [{summercms.attach summer_migrations_summercms_attach 202609180001_create_system_files ...} ...]
pending_test.go:321: [migrate:status --json]: the pending framework migration 202609180001_create_system_files is not listed
(same two failures for [migrate:status --json -q])
```
**WR-13** (TestMigrateRereadFailureWarns, Docker):
```
--- FAIL: TestMigrateRereadFailureWarns (0.42s)
pending_test.go:428: migrate: exit 1, want 0
ERROR lagoon: pending demo.rereadfirst: ERROR: column "id" does not exist (SQLSTATE 42703)
```
## Verification
| Check | Result |
|-------|--------|
| `go vet ./...` | pass |
| `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...` (Docker suite) | pass, 43 packages ok, exit 0. The wristband flake did not occur. |
| `go test -count=3 -race ./modules/bonfire/ ./modules/diary/` | pass (bonfire 15.6s, diary 2.2s) |
| Task 1 `-race -count=3` live-view set (TestPrintAboveAfterCancel, TestNestedProcessBuildsNoProgram, TestProcessViewStripsControl, TestPrintAboveOrderedNoLoss, TestPrintlnStaysWithItsConsole, TestLiveViewSingleOwner, TestLiveViewPanicUnregisters, TestLiveViewRestoresTerminalOnSignal, TestPrintAbove, TestLiveModels) | pass |
| `go -C examples/hello test -count=1 ./...` | pass |
| `scripts/check-phase19.8.sh self-test` | pass |
| `scripts/check-phase19.8.sh go` | pass (bonfire list now 44 tests, SUMMER list 7) |
| `scripts/check-phase19.8.sh docs` | pass |
| `scripts/check-phase19.8.sh docker` | pass (lagoon list now 7 tests, TestMigrateRereadFailureWarns included) |
| Task verify greps | `WithContext(` outside comments in live.go: 0; `strings.Join(argv` in runtime.go: 0; `argmask.Join(argv)` present; `migrationSets(gdb, plugins)` outside comments in migrations.go: 3; `open: 0`; `## IN-13` in the todo |
| gofmt on touched Go files | Clean in every hunk this task wrote. `gofmt -l` still lists `cmd/summer/runtime.go` and `cmd/summer/runtime_test.go`, but they were already unformatted at BASE (a struct field alignment at runtime.go:27 and a map alignment at runtime_test.go:81, both untouched by this task). Out of scope, not fixed. |
| `git status --short` | Only foreign files remain (STATE.md, state.json, .msd/, two .gitkeep files, this quick dir). Nothing of ours is unstaged, and nothing foreign was staged. |
The plugins, apps and landing stages of the gate were not run, as the plan says (they touch sibling repos).
## API check (no exported break)
`git diff BASE..HEAD -U0 -- modules cmd internal ':(exclude)*_test.go'`:
- removed-or-changed exported: **(empty)**
- added under modules/ exported: **(empty)**
Every changed declaration is unexported: bonfire's `secretWords`, `secretFlag` and `shellQuote` were removed (moved), and `watchContext` was added. `argmask.Join` and `argmask.Quote` are exported but sit in `internal/`, so they are not framework API. `lagoon.Status` keeps its signature, and `lagoon.StatusRow` keeps its fields.
## Decisions as applied
- **P-01 (WR-09):** Applied as designed. `tea.WithContext` was removed from `tea.NewProgram`, and `liveView` gained `done <-chan struct{}`, which is runLive's `finished` channel, created before the claim. The new unexported `watchContext(ctx, view, finished)` holds the watcher. On `ctx.Done()` it unregisters the view, sends `finishMsg{err: ctx.Err()}` and waits for `finished`, all under `liveMu`. On `finished` it only unregisters. In both branches it clears `activeView` only if the view is still the active one. The deferred cleanup now also waits for the watcher (`stopWatch()`) after releasing `liveMu`, so no goroutine outlives `runLive`. `printAbove` reads `activeView` into `v` and, after the owner check, does a non-blocking `case <-v.done:` that unregisters and returns false. watchSignals is unchanged. The fallback was not needed.
- **P-02 (IN-15):** Applied. `finished` is created before the lock, and the program is built under `liveMu` after the slot check.
- **P-03 (WR-14):** Applied at the call sites: `out.Success(Sanitize(item))` and `op := Sanitize(m.operation)`. `console.status` is unchanged.
- **P-04 (WR-11):** Applied. The `sync --token --force` case now expects `--token ***`, and the test comment explains why (pflag takes `--force` as the value).
- **P-05 (WR-10):** Applied. `internal/argmask` provides `Join` and `Quote` and imports only `strings`. bonfire keeps `tryArgs` as a thin wrapper. `runAppCmd` uses `argmask.Join(argv)` on every branch, a signal death still exits 1, and runtime.go's `strings` import was dropped. modules/bonfire is the first module to import a package under `internal/`; Go's internal rule allows it, because both sit in the same module root.
- **P-06 (WR-12):** Applied. The `migrate:status` Description and Examples are unchanged, and only the Long help changed. The goldens are untouched, since no golden carries the Long text. The `--json` loop in commands.go is unchanged.
- **P-07 (WR-13):** Applied. A `switch` takes the re-read result: on success it uses `appliedSince`, when Migrate and the re-read both fail it returns `errors.Join`, and when only the re-read fails it warns (`Could not re-read the migration history: <sanitized error>`) and keeps the pre-run pending list.
- **P-08:** Applied. TestNestedProcessBuildsNoProgram and TestPrintAboveAfterCancel joined the BONFIRE list, TestRunAppCmdMasksSecrets the SUMMER list, and TestMigrateRereadFailureWarns DOCKER_LAGOON_TESTS. Each joined in the same commit as its test.
- **P-09:** `docs/setup/upgrading.md` was not changed.
## Deviations from Plan
**1. [CLAUDE.md doc rule] The WR-10 commit also updates modules/bonfire/README.md**
- **Found during:** Task 2, after the WR-10 commit.
- **Issue:** CLAUDE.md says a change to a module's dependencies updates its README in the same commit. bonfire now imports `internal/argmask`, and the README's Dependencies section did not say so. The README's error-block paragraph also did not state the dash-leading masking rule.
- **Fix:** Added `internal/argmask` to the README's Dependencies line, and "also a value that starts with `-`" to the Try-line masking sentence. Folded into the WR-10 commit with `git commit --amend -- modules/bonfire/README.md` while it was still the newest commit, and before any hash was recorded anywhere. The final WR-10 hash is 17c722d.
- **Note:** The plan's WR-10 path list did not include the README. The WR-11 sentence in the README landed in the WR-10 commit rather than in the WR-11 commit; writing-commands.md got the WR-11 sentence in the WR-11 commit as planned.
**2. [Test detail] TestNestedProcessBuildsNoProgram checks the success line as "✓ x"**
- The plan says the "x" success line must reach the buffer. A bare `"x"` substring check would be weak, so the test matches `symCheck+" x"` in the stripped output. The behaviour pinned is unchanged.
**3. [Test detail] TestMigrateStatusJSON's pending entries use the table from HistoryTableName(ref.Plugin)**
- Implemented as planned. `attach.Migrations` holds one migration, `202609180001_create_system_files`, so "the newest attach row" is that one.
No other deviations. No finding was skipped.
## Skipped doc updates / follow-ups
- None skipped. None of the files this task edited had foreign uncommitted changes, which `git status --short -- <file>` checked before each edit.
- Pre-existing gofmt drift in `cmd/summer/runtime.go:27`, `cmd/summer/runtime_test.go:81` and `cmd/summer/main.go`, plus several cabana, compass, flare and tide files. It is not caused by this task and was not fixed, per the scope boundary. It may be worth a `/msd-fast` gofmt pass when no other session is editing cabana.
## Release-note input (P-09, for whoever tags next)
- `lagoon.Status` and `migrate:status` (table and `--json`) now list the framework's own migration sets first (`summercms.attach`, `summercms.cabana`, deferred bindings, queue), then each plugin with migrations. `--json` now shows a pending framework migration as `"applied": false`. A plugin whose `Migrations()` returns none no longer gets an empty row.
- `migrate` no longer exits 1 when the migrations succeeded but reading the history back failed. It prints a warning and counts the pending migrations as applied.
- When the context of a `bonfire.Process` view is cancelled, the view ends with its last frame and later lines print plainly below it. Before, they were lost.
- `bonfire.Process` strips control characters from item names on its plain path and from the operation label.
- Secret masking now also covers values that start with `-` (`--password -Xk9`), and summer's error for a delegate killed by a signal, or failing to start, masks secret flag values. A signal death still exits 1.
## Suggested g15office comment
> 19.8 re-review fixes landed locally (quick 261022-4z8). There are nine commits on master, not pushed: WR-09..WR-14, IN-14 and IN-15 are fixed with pinning tests, and IN-10..IN-13 plus the exit-130 idea are deferred to the console review todo. The disposition is at open: 0. Next: re-verify phase 19.8 to clear gaps_found, then a v0.4.x release on the user's go-ahead.
## Known Stubs
None.
## Threat Flags
None. No new network endpoint, auth path or schema change was added. The threat register's mitigations T-4z8-01..07 are each pinned by the tests listed above.
## Self-Check: PASSED
- FOUND: internal/argmask/argmask.go
- FOUND: 7c15095, 48e13f9, 1ac75ad, fce232d, 707372b, 17c722d, 57a849d, 0691d26, a48afbd (`git rev-list --count a28957f..a48afbd` = 9)
## Follow-up: WR-12 rework
Verification found that 57a849d changed what the exported `lagoon.Status` returns (framework sets first, no row for a plugin with no migrations). That broke `TestRollbackIsolatesFonotekaFullSchema` in `../fonoteka.go/parity`, so the plan's P-06 premise (no outside caller) was wrong. The orchestrator decided to keep `Status` as it was and to keep the WR-12 fix in the command only.
- **139f5e3** `fix(lagoon): keep Status plugin-only; migrate:status adds the framework sets itself (WR-12)`. `lagoon.Status` is back to its pre-57a849d behaviour: one row per plugin that implements `pact.HasMigrations`, in plugin order, with an empty row for a plugin with no migrations. `migrate:status` (table and `--json`) reads the new unexported `setStatus`, which takes its sets from `migrationSets`, so it still agrees with `migrate --pretend`. Both share the unexported `historyRows`. No exported API was added or changed. `TestTwoPluginMigrationSetsIsolated` and `TestDocsMigrate` are back to their pre-57a849d form. The new `TestStatusPluginRows` pins `Status`, and `TestMigrateStatusJSON` now also checks the table output. The new test is added to `scripts/check-phase19.8.sh`. The lagoon README and `docs/database/migrations.md` describe `Status` as plugin-only again.
- **b1fba7e** `docs(19.8): ...`. The WR-12 disposition row names both 57a849d and 139f5e3.
- **RED/GREEN.** In a scratch copy where the command calls the restored `Status` (that is, the command as it was before 57a849d), `TestMigrateStatusJSON` fails: the pending framework migration is missing from `--json`, and the table lists only the `demo.*` rows. In a scratch copy of the 57a849d code with the new tests, `TestStatusPluginRows`, `TestTwoPluginMigrationSetsIsolated` and `TestDocsMigrate` fail. All of them pass at 139f5e3.
- **Checks.** `go vet ./...` passes, as do `summer docs:build --check` and `scripts/check-phase19.8.sh self-test`. `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./modules/lagoon/...` and the full Docker `go test -count=1 ./...` exit 0.
- **fonoteka** (not modified, `replace => ../summercms.go`). `TestRollbackIsolatesFonotekaFullSchema` passes again. `TestMigrateSeedsCanonicalGenres` and `TestRollbackLastIsolatesFonoteka` still fail, and they did before this task: they now get past the row check (`status[0]` is `golem15.user`) but expect 11 user-plugin migrations, and the user plugin has 13.
- **Release-note correction.** The first bullet of "Release-note input" above is superseded. `lagoon.Status` does not change. Only `migrate:status` (table and `--json`) now lists the framework's own sets first, and it lists only plugins that have migrations.

View File

@@ -0,0 +1,168 @@
---
phase: quick-261022-4z8
verified: 2026-10-22T04:30:00Z
status: passed
score: 12/12 must-haves verified (WR-12 resolved by follow-up 139f5e3)
covered_files:
- .planning/phases/19.8-console-commands-overhaul/19.8-REVIEW-DISPOSITION.md
- .planning/quick/261022-4z8-fix-the-open-19-8-re-review-findings-wr-/261022-4z8-PLAN.md
- .planning/quick/261022-4z8-fix-the-open-19-8-re-review-findings-wr-/261022-4z8-SUMMARY.md
- .planning/todos/pending/2026-10-22-console-review-info-findings.md
- cmd/summer/runtime.go
- cmd/summer/runtime_test.go
- docs/console/introduction.md
- docs/console/setup-and-maintenance.md
- docs/console/writing-commands.md
- docs/database/migrations.md
- internal/argmask/argmask.go
- modules/bonfire/README.md
- modules/bonfire/errors.go
- modules/bonfire/errors_test.go
- modules/bonfire/helpers.go
- modules/bonfire/live.go
- modules/bonfire/live_test.go
- modules/lagoon/README.md
- modules/lagoon/commands.go
- modules/lagoon/example_test.go
- modules/lagoon/migrations.go
- modules/lagoon/migrations_test.go
- modules/lagoon/pending_test.go
- scripts/check-phase19.8.sh
covered_digest: "v2:sha256:a7e6e0cc40bcaa62cc411ab11ce6be4f7002259775e1e01c5e30862d9a068518"
behavior_unverified: 0
overrides_applied: 0
human_verification:
- test: "Decide whether the lagoon.Status contract change (WR-12) is acceptable as non-breaking, given that a sibling consumer depends on the old row set"
expected: "Either (a) accept it: update fonoteka.go/parity (withoutSharedPlugins or the index assertions) to the new framework-first rows, and record lagoon.Status's changed result in the next release notes / docs/setup/upgrading.md as a behaviour change; or (b) reject it: keep lagoon.Status plugin-only and add the framework sets only in migrate:status"
why_human: "The function signature and StatusRow fields are unchanged, so no compile-level API break exists; but the result set changed and the plan's P-06 premise ('No consumer outside this repo calls lagoon.Status') is false. fonoteka.go/parity calls lagoon.Status in three tests and asserts len(status)==2 and status[0].Plugin=='golem15.user'. Verified: TestRollbackIsolatesFonotekaFullSchema passes against summercms.go at a28957f and fails against a48afbd (status now starts with summercms.attach, summercms.cabana, summercms.deferred, summercms.conga). Whether that counts as an 'exported API break' under the task's constraint is a policy decision."
---
# Quick 261022-4z8: the open 19.8 re-review findings Verification Report
**Task goal:** Fix the open findings of the 19.8 code re-review (WR-09, WR-10, WR-11, WR-12, WR-13, WR-14, IN-14, IN-15). The constraints are one commit per finding, each with a test that fails without the fix; no exported API breaks; `go vet` and the Docker `go test ./...` pass; the disposition record and the info todo are updated; local commits only.
**Verified:** 2026-10-22T04:30:00Z
**Status:** human_needed
**Re-verification:** No (initial verification)
Every RED claim in SUMMARY.md was replayed independently. For each finding the parent of the fix commit was exported with `git archive` to the scratchpad, the test file(s) from the fix commit were overlaid, and the named test was run. The same test was then run on the fix commit itself. Every finding was red before its fix and green after it.
## Goal Achievement
### Observable Truths
| # | Truth | Status | Evidence |
|---|-------|--------|----------|
| 1 | WR-09: cancelling Process's ctx ends the view with its last frame, unregisters it without waiting for work; later Println/Info/PrintAbove print plainly (PrintAbove false), none lost; printAbove refuses a program whose Run ended on its own | ✓ VERIFIED | live.go: `tea.WithContext` removed (no non-comment `WithContext(`); `watchContext` unregisters, sends `finishMsg{ctx.Err()}` and waits for `finished` under liveMu; `liveView.done`; `printAbove` has `case <-v.done:`. TestPrintAboveAfterCancel: RED at fce232d^ (the view was still registered after 2s, PrintAbove returned true, and all three lines were printed 0 times), GREEN at fce232d. `-race -count=10` on the live-view set passes. bubbletea v2.1.0 `Send` selects on `p.ctx.Done()` and `Run` does `defer p.cancel()`, so the deferred cleanup's Send after the watcher finishes cannot block. The "ended on its own" branch was confirmed by a throwaway verifier probe in a scratch copy (activeView with a closed `done`: printAbove returns false and unregisters). No committed test pins that branch (Info). |
| 2 | WR-14: Process's plain path prints item names through Sanitize; the view's operation label is sanitized | ✓ VERIFIED | helpers.go:183 `out.Success(Sanitize(item))`; live.go:313 `op := Sanitize(m.operation)`. TestProcessViewStripsControl: RED at 48e13f9^ (label, both plain-path subtests), GREEN at 48e13f9. |
| 3 | IN-15: runLive claims the slot before building the program; the loser builds none (no child ctx, no TEA_TRACE file) | ✓ VERIFIED | live.go: the `activeView != nil` check under liveMu comes before `tea.NewProgram`. TestNestedProcessBuildsNoProgram: RED at 1ac75ad^ (the trace file existed and 1 child ctx stayed registered), GREEN at 1ac75ad. |
| 4 | IN-14: the ErrReported doc comment names the WithExitCode code | ✓ VERIFIED | errors.go:74-76. The `var ErrReported` line is unchanged. This finding only changes a doc comment, so no test is possible. |
| 5 | WR-11: the Try line masks the argument after a secret-named long flag whatever its first character; only `--` is left alone | ✓ VERIFIED | argmask.go `args[i+1] != "--"`. TestWrongDirectoryMasksSecrets: RED at 707372b^ (`-Xk9!q` and `-s3cr3t` in clear text), GREEN at 707372b. |
| 6 | WR-10: the error for a signal-killed or unstartable delegate masks argv through the shared internal/argmask; exit 1 is kept; no exported identifier changes | ✓ VERIFIED | runtime.go:174 `argmask.Join(argv)` wraps every branch, and no `strings.Join(argv` remains. bonfire's tryArgs and cd line use `argmask.Join` and `argmask.Quote`; bonfire's own secretWords, secretFlag and shellQuote are gone, so only one implementation exists. TestRunAppCmdMasksSecrets and TestDelegateExitCodePassthrough/killed_by_a_signal_masks_secrets: RED at 17c722d^ (hunter2, t0k3n and -Xk9q in the error), GREEN at 17c722d. |
| 7 | WR-12: lagoon.Status reads migrationSets (framework sets first); --json lists every pending migration including framework ones; the signature and StatusRow are unchanged | ✓ VERIFIED | migrations.go:150. `migrationSets(gdb, plugins)` appears 3 times outside comments (Migrate, Status, PendingMigrations). TestMigrateStatusJSON (Docker): RED at 57a849d^ (pending `summercms.attach 202609180001_create_system_files` missing), GREEN at 57a849d. See truth 12 for the consumer impact. |
| 8 | WR-13: when Migrate succeeds but the re-read fails, migrate warns, reports the pre-run pending list as applied and exits 0; when both fail, both errors are returned | ✓ VERIFIED | commands.go:177-192 `switch`: `err == nil` gives appliedSince; `migrateErr != nil` gives `errors.Join`; otherwise it warns with `Could not re-read the migration history`. TestMigrateRereadFailureWarns (Docker): RED at 0691d26^ (exit 1), GREEN at 0691d26. The both-fail branch is checked by reading the code only; it is a one-line return kept from before. |
| 9 | One commit per finding with its pinning test and docs; no Co-Authored-By trailer; nothing pushed or tagged | ✓ VERIFIED | `git log a28957f..a48afbd` shows 9 commits: 8 finding commits (IN-14, WR-14, IN-15, WR-09, WR-11, WR-10, WR-12, WR-13) and 1 docs(19.8) records commit. `grep -ci co-authored` = 0. No tag points at or contains these commits. The branch is `ahead 16` of origin/master. Minor deviation: modules/bonfire/README.md's WR-11 masking sentence landed in the WR-10 commit (recorded in SUMMARY). |
| 10 | The disposition marks WR-09..WR-14, IN-14 and IN-15 fixed with hashes (open: 0) plus the WR-05/07/08 remainders, and IN-10..IN-13 deferred; the todo carries IN-10..IN-13 and the exit-130 suggestion | ✓ VERIFIED | 19.8-REVIEW-DISPOSITION.md has `open: 0`, `total: 32`, and table rows 143-154 and 162-165 with the matching hashes. The todo has `## IN-10`..`## IN-13` and `## WR-10 follow-up: exit code 128+n …`, and its title and files are updated. |
| 11 | `go vet ./...` and the Docker `go test ./...` pass | ✓ VERIFIED | `go vet ./...` passes. `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 go test -count=1 ./...` exits 0 with 43 packages ok (lagoon 40s and lagoon/attach 54s, so the Docker tests ran). |
| 12 | No exported API breaks | ? UNCERTAIN | The compile-level API is intact: the diff shows no exported func, type, var or const removed or changed, and none added under modules/ (argmask.Join and argmask.Quote are in internal/). However, **lagoon.Status's result changed** (framework rows first; a plugin with zero migrations no longer gets a row). The plan's P-06 premise that no outside consumer calls it is false: `fonoteka.go/parity/migrate_test.go` (3 calls) and `rollback_isolation_full_test.go` call it, and fonoteka uses `replace => ../summercms.go`. Reproduced: TestRollbackIsolatesFonotekaFullSchema PASSES against a28957f and FAILS against a48afbd (`status = [summercms.attach …]`, `len != 2`). Needs a human decision (see Human Verification). |
**Score:** 11/12 truths verified (0 present but behavior-unverified, 1 uncertain)
### Required Artifacts
| Artifact | Expected | Status | Details |
|----------|----------|--------|---------|
| `modules/bonfire/live.go` | slot claimed before NewProgram; watcher; liveView.done; printAbove refuses a finished program | ✓ VERIFIED | contains `case <-v.done:`; watchContext wired in runLive |
| `modules/bonfire/helpers.go` | Sanitize on plain path | ✓ VERIFIED | `out.Success(Sanitize(item))` |
| `modules/bonfire/errors.go` | ErrReported comment; tryArgs via argmask | ✓ VERIFIED | `argmask.Join`, `argmask.Quote` |
| `internal/argmask/argmask.go` | Join + Quote, stdlib only | ✓ VERIFIED | imports only `strings`; used by bonfire and cmd/summer |
| `cmd/summer/runtime.go` | runAppCmd uses argmask.Join(argv) | ✓ VERIFIED | line 174 |
| `modules/lagoon/migrations.go` | Status from migrationSets, signature unchanged | ✓ VERIFIED | line 146 signature identical |
| `modules/lagoon/commands.go` | re-read warning | ✓ VERIFIED | `Could not re-read the migration history` |
| `modules/bonfire/live_test.go` | TestPrintAboveAfterCancel, TestNestedProcessBuildsNoProgram, plain-path cases | ✓ VERIFIED | present, red-before/green-after replayed |
| `cmd/summer/runtime_test.go` | TestRunAppCmdMasksSecrets + masked signal subtest | ✓ VERIFIED | present, replayed |
| `modules/lagoon/pending_test.go` | TestMigrateStatusJSON framework pending, TestMigrateRereadFailureWarns | ✓ VERIFIED | present, replayed with Docker |
| `19.8-REVIEW-DISPOSITION.md` | open: 0 | ✓ VERIFIED | |
### Key Link Verification
| From | To | Via | Status | Details |
|------|----|-----|--------|---------|
| cmd/summer/runtime.go | internal/argmask | `argmask.Join(argv)` | WIRED | the only argv rendering in runAppCmd |
| modules/bonfire/errors.go | internal/argmask | `argmask.Join` / `argmask.Quote` | WIRED | the old local helpers were deleted |
| lagoon Migrate/Status/PendingMigrations | migrationSets | `migrationSets(gdb, plugins)` | WIRED | 3 call sites |
| modules/bonfire/live.go | helpers.go Process | ctx → runLive → watchContext; printAbove `case <-v.done` | WIRED | |
| helpers.go | Sanitize | `Sanitize(item)` | WIRED | |
### Behavioral Spot-Checks
| Behavior | Command | Result | Status |
|----------|---------|--------|--------|
| RED/GREEN replay per finding (WR-14, IN-15, WR-09, WR-11, WR-10) | `git archive <fix>^` + overlay the fix commit's test file(s), `go test -run <test>`; then the same at `<fix>` | all red before the fix, all green after | ✓ PASS |
| RED/GREEN replay WR-12, WR-13 (Docker) | same, with `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1` | red before the fix (framework pending missing; exit 1), green after | ✓ PASS |
| bonfire + diary under race | `go test -count=3 -race ./modules/bonfire/ ./modules/diary/` | ok | ✓ PASS |
| live-view set stress | `go test -count=10 -race -run 'TestPrintAboveAfterCancel\|TestNestedProcessBuildsNoProgram\|TestPrintAboveOrderedNoLoss\|TestLiveViewSingleOwner\|TestLiveViewPanicUnregisters\|TestLiveViewRestoresTerminalOnSignal'` | ok (49s) | ✓ PASS |
| printAbove ended-program branch | throwaway probe test in a scratch copy (not committed) | PASS | ✓ PASS |
| hello workspace | `go -C examples/hello test -count=1 ./...` | ok | ✓ PASS |
| Downstream consumer (fonoteka parity) | copies of fonoteka.go against summercms.go at a28957f and at a48afbd | TestRollbackIsolatesFonotekaFullSchema passes at base and fails at head; the other two Status tests already fail at base (user plugin migration count 13 vs 11, unrelated drift) and fail earlier at head | ✗ regression in a sibling repo (see truth 12) |
### Probe Execution
| Probe | Command | Result | Status |
|-------|---------|--------|--------|
| `scripts/check-phase19.8.sh self-test` | `bash scripts/check-phase19.8.sh self-test` | exit 0 | PASS |
| `scripts/check-phase19.8.sh go` | `bash scripts/check-phase19.8.sh go` | exit 0 | PASS |
| `scripts/check-phase19.8.sh docs` | `bash scripts/check-phase19.8.sh docs` | exit 0 | PASS |
| `scripts/check-phase19.8.sh docker` | with `TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1` | exit 0 | PASS |
### Requirements Coverage
| Requirement | Source Plan | Status | Evidence |
|-------------|-------------|--------|----------|
| WR-09 | 261022-4z8-PLAN | ✓ SATISFIED | truth 1 |
| WR-10 | 261022-4z8-PLAN | ✓ SATISFIED | truth 6 |
| WR-11 | 261022-4z8-PLAN | ✓ SATISFIED | truth 5 |
| WR-12 | 261022-4z8-PLAN | ✓ SATISFIED (consumer impact: truth 12) | truth 7 |
| WR-13 | 261022-4z8-PLAN | ✓ SATISFIED | truth 8 |
| WR-14 | 261022-4z8-PLAN | ✓ SATISFIED | truth 2 |
| IN-14 | 261022-4z8-PLAN | ✓ SATISFIED | truth 4 |
| IN-15 | 261022-4z8-PLAN | ✓ SATISFIED | truth 3 |
| 4z8-docs | 261022-4z8-PLAN | ✓ SATISFIED | truth 10 |
### Anti-Patterns Found
| File | Line | Pattern | Severity | Impact |
|------|------|---------|----------|--------|
| (added lines in a28957f..a48afbd) | none | TBD/FIXME/XXX/TODO/HACK | none | no debt markers added |
| cmd/summer/runtime.go, cmd/summer/runtime_test.go | 27, 81 | gofmt drift | ℹ️ Info | already present at a28957f (checked); not introduced by this task |
| modules/bonfire/live.go | printAbove `case <-v.done` | branch not pinned by a committed test | ℹ️ Info | behaviour confirmed by a verifier probe; a small committed test would pin it |
| modules/lagoon/migrations.go | Status | consumer-visible result change with no upgrade note | ⚠️ Warning | P-09 deliberately skipped docs/setup/upgrading.md; the SUMMARY's release-note input mentions it, but the false "no outside consumer" premise means fonoteka.go needs a follow-up |
### Human Verification Required
### 1. Accept or reject the lagoon.Status contract change
**Test:** Decide whether WR-12's change to `lagoon.Status` (framework sets first; a plugin with zero migrations no longer gets a row) is acceptable under "no exported API breaks".
**Expected:** If you accept it, update `fonoteka.go/parity` (`withoutSharedPlugins` should also drop the `summercms.*` rows, or the index assertions should change) and note the behaviour change in the next release notes and upgrade docs. If you reject it, keep `Status` plugin-only and add the framework sets inside the `migrate:status` command instead.
**Why human:** This is a policy call. The Go signature is unchanged. But a sibling consumer (fonoteka.go, `replace => ../summercms.go`) calls `lagoon.Status` and now fails `TestRollbackIsolatesFonotekaFullSchema`, which passes against the pre-task framework. The plan's statement that no outside consumer exists was wrong.
### Gaps Summary
No code gap. All eight findings are fixed as the review asked. Each has a pinning test that was independently proven red on the parent commit and green on the fix. The shared masking is in one place (`internal/argmask`). `go vet`, the full Docker suite, the race runs, the hello workspace and the four phase-gate stages all pass. The records are updated, and the commits are local, untagged and without trailers.
One item needs a human decision. The WR-12 fix changes what `lagoon.Status` returns. That is a behavioural, not compile-level, change to an exported function, and it breaks one previously passing test in the sibling consumer `fonoteka.go/parity`, contrary to the plan's premise. Note also that fonoteka's other two Status-based parity tests were already failing before this task (user plugin migration count drift), so fonoteka's parity suite was not green either way.
---
_Verified: 2026-10-22T04:30:00Z_
_Verifier: Claude (msd-verifier)_
## Resolution of the human_needed item (orchestrator, 2026-10-22)
The open item was the WR-12 behaviour change to the exported `lagoon.Status`, which broke
`fonoteka.go/parity` `TestRollbackIsolatesFonotekaFullSchema`. Decision (no consumer breaks):
keep `lagoon.Status` plugin-only as before 57a849d; `migrate:status` builds its rows from the
shared `migrationSets` through the unexported `setStatus`. Follow-up commits `139f5e3` (fix,
`TestStatusPluginRows` pins `Status`, `TestMigrateStatusJSON` still red without the fix) and
`b1fba7e` (disposition). Full Docker `go test ./...` green; the fonoteka test passes again. The two
other fonoteka `Status` tests fail as before this task (they expect 11 user-plugin migrations, the
plugin now has 13) — unrelated, pre-existing.

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,144 @@
---
created: 2026-10-22T00:43:48.000Z
title: Console review info findings (IN-01..IN-13) and the delegate signal exit code
area: framework
severity: minor
g15office: rwb3fd9p
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
- modules/bonfire/live.go
- modules/bonfire/output.go
- cmd/summer/runtime.go
---
## 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. The re-review of those fixes (same file, gap-closure range dd12382..0ebed68) found six warnings and six info findings; quick task 261022-4z8 fixed the warnings, IN-14 and IN-15, and deferred IN-10..IN-13 and WR-10's exit-code suggestion to this todo (the sections after IN-09).
## 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.
## IN-10: console.Error during a live view writes straight to the terminal and tears the view
**File:** `modules/bonfire/output.go` (`console.Error`, `emit`)
**Problem:** `Error` writes to `c.err`, and `emit` reroutes only writes to `c.out`. When stderr and stdout are the same terminal (the usual case), an `out.Error(...)` from inside `Spinner`, `Progress` or `Process` lands mid-frame. A pty run showed the spinner glyph left on the error line (`⠹ ✗ stderr-error-line`), with the renderer continuing on the next row. diary's console records on stderr do go above the view through `PrintAbove`, so the two stderr paths behave differently.
**Suggested fix:** In `emit`, also reroute `w == c.err` when the console owns the running view and `c.err` is a terminal (`printAbove(c, s)`).
## IN-11: A blank line printed during a live view is dropped
**File:** `modules/bonfire/live.go` (`printAbove`, the `strings.TrimSuffix` of the block)
**Problem:** `out.Println()` gives `"\n"`. `TrimSuffix` turns that into `""`, and bubbletea's `insertAbove` returns early for an empty string (cursed_renderer.go in bubbletea v2.1.0). `printAbove` still reports `true`, so the blank separator line is lost. It is printed when no view runs.
**Suggested fix:** Send `" "` (or `"\u200b"`) for an empty block, or skip the trim when the block is only a newline.
## IN-12: Signal relay edge cases: a signal during view start-up, and Windows
**File:** `modules/bonfire/live.go` (`runLive`, `watchSignals`, `raise`)
**Problem:** This comes from reading bubbletea v2.1.0; the reviewer did not reproduce it.
1. **Start-up race.** `prog.Run` starts on its own goroutine just before the relay is registered. If a signal arrives before `Run` has created and started its renderer, `Kill` runs `shutdown(true)` while `p.renderer` is still nil. `shutdownOnce` then makes `Run`'s own `shutdown(killed)` a no-op after it has started the renderer and hidden the cursor, so the cursor stays hidden.
2. **Windows.** On Windows, `p.Signal(os.Interrupt)` fails, so `raise` calls `os.Exit(130)`. That skips the command's own handler (for example, `summer dev` stopping its serve child) and its deferred cleanup.
**Suggested fix:** Before killing, wait until the program reports it has started (for example, have the wrapped model's `Init` close a `ready` channel). On platforms where `raise` cannot deliver the signal, return without exiting when another handler is registered.
## IN-13: A delegate's stderr always goes to os.Stderr, bypassing the root's error writer
**File:** `cmd/summer/runtime.go` (`appStreams`)
**Problem:** Off a terminal, the child's stdout goes through `out`, but its stderr goes to the real `os.Stderr`, not to the error stream of the root that ran the command. For a summer root built with `NewRootIO(…, errW)` (tests, embedding), the app binary's error block cannot be captured. `TestDelegateExitCodePassthrough` therefore cannot check that the binary's own block was printed.
**Suggested fix:** Pass the root's error writer through (for example, a `bonfire.ErrWriter(out)` accessor), defaulting to `os.Stderr`.
## WR-10 follow-up: exit code 128+n for a delegate killed by a signal
**File:** `cmd/summer/runtime.go` (`runAppCmd`)
**Problem:** Plan 19.8-05 specified exit code 1 for an app binary that dies by a signal, and quick task 261022-4z8 kept it while masking the argv in summer's error block (WR-10). The re-review points out that Ctrl+C is the normal way to stop `summer serve` or `summer queue:work`: since the WR-01 relay re-raises SIGINT, a child that dies by the signal produces an ERROR block and exit 1, where shells and CI expect 130 (128+SIGINT).
**Suggested fix:** Map signal deaths to 128+n: treat SIGINT and SIGTERM as already reported (`bonfire.Reported(bonfire.WithExitCode(err, 128+n))`, no block), and give other signals exit 128+n with summer's masked block (`argmask.Join(argv)`). `docs/console/introduction.md` documents exit code 1 for a signal death and must change with it, as must `TestDelegateExitCodePassthrough`'s signal subtests.

View File

@@ -0,0 +1,50 @@
---
created: 2026-10-22T03:30:00.000Z
title: User plugin logout leaves the session cookie valid for up to 60 minutes
area: user-plugin
severity: major
g15office: ttf7tinw
files:
- ../sm-user-plugin (mounted at plugins/golem15/user in the apps)
- controllers/api_controller.go
- classes/session_user.go
- routes.go
---
## Problem
Reported by the user: after logout the auth cookie is not invalidated at once; requests keep
authenticating until the token expires about 60 minutes later. If true, a logout on a shared
machine does not end the session, and a stolen cookie survives the victim's logout.
## To check
- What logout does: clear the cookie only, or also revoke the server-side session / refresh
token, and whether the access JWT is checked against a revocation list (bouncer) or only by
signature and `exp`.
- Whether the 60 minutes is the access-token TTL.
- Parity with the PHP (WinterCMS) user plugin's logout behaviour; the frontend contract stays.
## Fix direction
Logout revokes the server-side session so the access token is refused on the next request
(same mechanism c3712dc uses for deactivate/ban), clears the cookies with matching
attributes, and a test pins "request after logout is 401". No breaking change to the plugin
API (core plugin, many projects).
## Outcome (2026-10-22)
Confirmed and fixed locally; closes when released. Debug session:
`.planning/debug/user-logout-session-stays-valid.md`.
- Plugin root cause: `Logout` read the token from the `Authorization` header only, so a
cookie-only logout (sociallogin `token_cookie: true`) answered 401 and revoked nothing, and the
cookie kept working for the 60-minute access TTL; logout cleared no cookie; the blacklist row
lived only until `exp`, so a logged-out token could be refreshed again once it was swept.
Fix: sm-user-plugin `358ee5d` (new `classes.EndSession`, blacklist until the end of the refresh
window, forget cookies with the configured attributes, `TestLogoutEndsTheSession`).
- Demo frontend root cause: `useDemoAuth().logout()` only cleared its own cookie and never called
`/logout`. Fix: vue-demo-app `f397df2`.
- Ships in sm-user-plugin v0.1.8 (with `aa83969`, needs SummerCMS v0.4.3) and the sm-demo-app
pointer bumps. Hand check after deploy: cookie-only logout, then a `jwt.auth` route with the old
cookie answers 401.

View File

@@ -9,11 +9,11 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"git.golem15.com/golem15/summercms/internal/argmask"
"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,64 @@ 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. The
// error names the command through argmask.Join, with the values of
// secret-looking flags masked, on every branch: a binary that fails to
// start or is ended by a signal gets summer's error block, and a signal
// death keeps exit code 1.
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", argmask.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,233 @@ 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, WR-10): 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, with secret flag values
// masked, 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, args ...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, args)
return out.String(), errW.String(), code
}
t.Run("exit code", func(t *testing.T) {
out, errOut, code := run(t, "exit2", "migrate", "--bogus")
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", "migrate", "--bogus")
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)
}
})
t.Run("killed by a signal masks secrets", func(t *testing.T) {
out, errOut, code := run(t, "sigkill", "migrate", "--password", "hunter2")
if code != bonfire.ExitFailure {
t.Errorf("exit %d, want 1", code)
}
if n := strings.Count(errOut, " ERROR "); n != 1 || !strings.Contains(errOut, "--password ***") {
t.Errorf("want one error block with the password masked, got %d:\nstdout %q\nstderr %q", n, out, errOut)
}
if strings.Contains(out+errOut, "hunter2") {
t.Errorf("the password reached the output:\nstdout %q\nstderr %q", out, errOut)
}
})
}
// TestRunAppCmdMasksSecrets (WR-10): the error of an app binary killed by a
// signal or failing to start names the command with the values of
// secret-named flags masked, as the wrong-directory Try line does.
func TestRunAppCmdMasksSecrets(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("SIGKILL is POSIX-only")
}
argv := []string{"admin:create", "--email", "a@b.c", "--password", "hunter2", "--api-token=t0k3n", "--client-secret", "-Xk9q"}
check := func(t *testing.T, err error, want ...string) {
t.Helper()
if err == nil {
t.Fatal("runAppCmd returned nil")
}
if errors.Is(err, bonfire.ErrReported) {
t.Errorf("error is marked as reported: %v", err)
}
msg := err.Error()
for _, w := range append(want, "--password ***", "--api-token=***", "--client-secret ***") {
if !strings.Contains(msg, w) {
t.Errorf("error lacks %q: %s", w, msg)
}
}
for _, secret := range []string{"hunter2", "t0k3n", "-Xk9q"} {
if strings.Contains(msg, secret) {
t.Errorf("error repeats %q: %s", secret, msg)
}
}
}
t.Run("killed by a signal", func(t *testing.T) {
cmd := newAppCmd(context.Background(), os.Args[0], ".", []string{"-test.run=^TestExitHelperProcess$"}, io.Discard, io.Discard)
cmd.Env = append(cmd.Env, "SUMMER_EXIT_HELPER=sigkill")
check(t, runAppCmd(cmd, argv), "signal: killed")
})
t.Run("start failure", func(t *testing.T) {
cmd := newAppCmd(context.Background(), filepath.Join(t.TempDir(), "missing"), ".", argv, io.Discard, io.Discard)
check(t, runAppCmd(cmd, argv))
})
}
// 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

@@ -30,7 +30,8 @@ Some column names are never written from a request body, whatever the form decla
```go src=modules/cabana/example_form_seams_test.go#MembersController.AdminFieldRelations
// AdminFieldRelations binds the form's two relation fields. organisation_id
// is a protected column, so the team field would be read-only; the contract
// opts in to writing it through this field.
// opts in to writing it through this field. A save that moves the member to
// another team (or to none) clears team_role, unless it writes a new one.
func (MembersController) AdminFieldRelations() []cabana.FieldRelationContract {
return []cabana.FieldRelationContract{{
Field: "team",
@@ -38,6 +39,7 @@ func (MembersController) AdminFieldRelations() []cabana.FieldRelationContract {
NewRelated: func() any { return &Team{} },
ForeignKey: "organisation_id",
WritableForeignKey: true,
ClearOnChange: []string{"team_role"},
}, {
Field: "groups",
Kind: "belongsToMany",
@@ -51,6 +53,12 @@ func (MembersController) AdminFieldRelations() []cabana.FieldRelationContract {
The protection itself does not change. A plain field named `organisation_id` is still dropped from a body, and the id submitted through the relation field is still rechecked against the scoped options query, so only a record the controller offers can be set. `WritableForeignKey` on a belongsToMany contract stops the start-up.
### Clearing dependent columns
Some columns only make sense for one parent: a member's role in an organisation means nothing in the next organisation. `ClearOnChange` on a belongsTo contract names such columns of the record; a save that changes the field's foreign key, to another record or to none, sets them to NULL, so the role never moves along with the member. The team contract above clears `team_role` this way.
A save that keeps the key leaves the columns alone, and so does a save that writes a column through its own form field: an administrator who moves a member and picks the new role in the same save keeps that role. Each column must be a nullable column of the record (a pointer field), and neither the foreign key nor the primary key; anything else, or `ClearOnChange` on a belongsToMany contract, stops the start-up. The zero value changes nothing. A hasMany relation manager has the same option on `cabana.RelationContract` (see [Relation kinds](#relation-kinds)).
### Locked options
A controller may let an administrator see a choice without letting them change it, for example membership of a group that grants access. It implements `cabana.RelationLockProvider` and returns, per field and per request, a `cabana.RelationLock`: the related ids that are locked for the signed-in administrator, and a message.
@@ -132,6 +140,8 @@ func (PostsController) AdminRelationContracts() []cabana.RelationContract {
A hasMany `ForeignKey` of a pointer type (`*uint`, a nullable column) is needed for `unlink` and for managing the relation before the parent is saved.
A hasMany contract may also name `ClearOnChange` columns of the related model: nullable columns that a link and an unlink set to NULL together with the `ForeignKey`, deferred links and unlinks included. With an organisation's `members` relation and `ClearOnChange: []string{"organisation_role"}`, a user added to or removed from the organisation never keeps a role from elsewhere. The rules are those of the belongsTo option: a pointer field, neither the `ForeignKey` nor the primary key, and only on a hasMany; the zero value changes nothing.
### Relation forms
The relation manager creates and edits related records in a modal through a form of their own. `manage.form` names the form used to create and update a child, `view.form` the read-only preview; a top-level `form` is the fallback of both, as in WinterCMS:
@@ -187,7 +197,7 @@ The `manage` panel may declare only `link`. An unknown or duplicate button stops
What delete, link and unlink do depends on the kind:
- On a hasMany, `delete` deletes the child through its model, so its hooks and soft delete run. `link` adopts records whose `ForeignKey` is NULL, setting it to the parent's key through the child model; `unlink` sets it back to NULL.
- On a hasMany, `delete` deletes the child through its model, so its hooks and soft delete run. `link` adopts records whose `ForeignKey` is NULL, setting it to the parent's key through the child model; `unlink` sets it back to NULL. Both also set the contract's `ClearOnChange` columns to NULL.
- On a belongsToMany, `delete` removes this parent's pivot row and then deletes the related record through its model. `link` writes pivot rows and `unlink` deletes them, as before.
Every child route is scoped to its parent. The parent is loaded through `pact.FormExtendQuery`, and the child must belong to it: on a hasMany by its `ForeignKey`, on a belongsToMany by a pivot row. A child of another parent, or any child of a parent the administrator cannot open, answers 404 `not_found`, never 403, so a request cannot tell a foreign record from a missing one. A delete is all or nothing: when one of the ids is not a child of the parent, nothing is deleted.

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`; the block names the command with the values of secret-looking flags (`--password`, `--api-token=...`) masked as `***`.
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

@@ -12,9 +12,9 @@ Every application binary carries the framework's runtime commands. The examples
| Command | Flags | Purpose |
|---------|-------|---------|
| `migrate` | `--pretend`, `--force` | Runs the framework migrations, then every plugin's migrations in dependency order, printing how many each set applied and `Applied N migrations` (or `Nothing to migrate`). `--pretend` lists the pending migrations as `<plugin>: <id>` and applies nothing. |
| `migrate` | `--pretend`, `--force` | Runs the framework migrations, then every plugin's migrations in dependency order, printing how many each set applied and `Applied N migrations` (or `Nothing to migrate`). If the history cannot be re-read after a successful run, it warns and counts every pending migration as applied. `--pretend` lists the pending migrations as `<plugin>: <id>` and applies nothing. |
| `migrate:rollback` | `--plugin <id>`, `--pretend`, `--force` | Rolls back the last migration of the plugin; without the flag, of the last activated plugin that has migrations. Prints `Rolled back <id> of <plugin>`; `--pretend` only names it. |
| `migrate:status` | `--json` | Prints a table of plugin, history table and applied migration IDs. `--json` prints every declared migration of those plugins with `plugin`, `table`, `id` and `applied`. |
| `migrate:status` | `--json` | Prints a table of the framework's own migration sets and every plugin with migrations, with history table and applied migration IDs. `--json` prints every declared migration of those sets with `plugin`, `table`, `id` and `applied`. |
```sh
./bin/acme migrate --pretend

View File

@@ -74,7 +74,7 @@ Print the data your command exists to produce (a token, a key, JSON) with `bonfi
Before a destructive step, ask with `bonfire.ConfirmProceed`. It asks only on an interactive terminal; off a terminal, with `-n` or `-q`, and when the scheduler calls the command, it returns true without asking, so deploys over SSH never wait for an answer.
For a loop over many items, `bonfire.Process` runs a function per item and shows SSU's process view on a terminal, one row per item with its state, or prints one `✓ item` or `✗ item: error` line per item elsewhere; it returns the failures joined. Inside `bonfire.Output.Progress`, `bonfire.Current` names the item being worked on and `bonfire.Failed` records a failed one. Log lines and output printed while a live view runs appear above the view (`bonfire.PrintAbove`).
For a loop over many items, `bonfire.Process` runs a function per item and shows SSU's process view on a terminal, one row per item with its state, or prints one `✓ item` or `✗ item: error` line per item elsewhere; it returns the failures joined. Inside `bonfire.Output.Progress`, `bonfire.Current` names the item being worked on and `bonfire.Failed` records a failed one. Log lines and output printed while a live view runs appear above the view (`bonfire.PrintAbove`). When the context given to `bonfire.Process` is cancelled, the view ends with its last frame and later lines print plainly below it.
Return an error from `Run` to fail the command. The binary prints it as an error block on stderr and exits with status 1; see [Errors and hints](#errors-and-hints).
@@ -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 ***` (also a value that starts with `-`), `--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

@@ -81,7 +81,7 @@ The application binary has the migration commands:
`migrate:rollback` rolls back the last applied migration of one plugin. Without `--plugin` it picks the last activated plugin that has migrations. To fix a migration you just wrote, roll it back, edit it and run `migrate` again.
`migrate:status` prints each plugin's history table and applied IDs.
`migrate:status` prints the history table and applied IDs of the framework's own sets (under history IDs such as `summercms.attach`) and of each plugin with migrations.
The same operations are Go functions, which is how tests migrate a fresh database:
@@ -104,6 +104,8 @@ if err := lagoon.RollbackLast(db, plugins, "acme.blog"); err != nil {
`lagoon.Migrate`, `lagoon.Status` and `lagoon.RollbackLast` take the activated plugins; a plugin that does not implement `pact.HasMigrations` is skipped.
`lagoon.Status` returns one row per plugin, a plugin with no migrations included, and leaves out the framework's own sets that `migrate:status` prints.
## Porting version.yaml
WinterCMS runs a plugin's update scripts by version number and seeds data from the same files. When you port a plugin:

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

@@ -0,0 +1,63 @@
// Package argmask renders a command line for messages with the values of
// secret-named long flags masked; bonfire's wrong-directory hint and
// summer's delegate errors share it.
package argmask
import "strings"
// secretWords mark a long flag whose value is masked.
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
}
// Join renders args as one line, separated by single spaces and each
// quoted for a shell (Quote). The value of a long flag named like a secret
// becomes ***, in the "--flag=value" form and in the "--flag value" form,
// so a password typed on the command line does not reach stderr and CI
// logs. In the "--flag value" form the next argument is masked whatever its
// first character (pflag takes "-Xk9" as the value too); only "--" is left
// alone. Join cannot see flag types, so a secret-named boolean flag
// over-masks the argument after it, which is the safe side.
func Join(args []string) string {
var b strings.Builder
for i := 0; i < len(args); i++ {
if i > 0 {
b.WriteByte(' ')
}
a := args[i]
name, long := strings.CutPrefix(a, "--")
flag, _, inline := strings.Cut(name, "=")
if !long || flag == "" || !secretFlag(flag) {
b.WriteString(Quote(a))
continue
}
if inline {
b.WriteString(Quote("--"+flag) + "=***")
continue
}
b.WriteString(Quote(a))
if i+1 < len(args) && args[i+1] != "--" {
b.WriteString(" ***")
i++
}
}
return b.String()
}
// Quote single-quotes s when a shell would split or expand it.
func Quote(s string) string {
if s != "" && !strings.ContainsAny(s, " \t\n'\"\\$`*?[]{}()<>|&;#~!") {
return s
}
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
}

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. When the context given to `bonfire.Process` is cancelled, the view ends with its last frame and later lines print plainly below it. Table cells, the item names and errors of `bonfire.Process` (in the process view and in its plain lines), the process view's operation label, 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, also a value that starts with `-`.
- 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`. |
@@ -189,7 +191,7 @@ Without these variables, color is enabled only when stdout is a terminal. `--ans
## Dependencies
- SummerCMS modules: `sunscreen` (scrubs error messages).
- SummerCMS modules: `sunscreen` (scrubs error messages); the framework-internal package `internal/argmask` (masks secret flag values in the Try line, shared with summer's delegate errors).
- Third-party: `github.com/spf13/cobra`, `github.com/spf13/pflag`, `golang.org/x/term`, `charm.land/lipgloss/v2` (with `lipgloss/v2/table`), `charm.land/bubbletea/v2`, `charm.land/bubbles/v2` (spinner, progress, key), `charm.land/huh/v2`, `charm.land/fang/v2` and `github.com/charmbracelet/colorprofile` (decided in `.planning/notes/2026-10-21-console-charm-stack.md`).
- Standard library: `bufio`, `context`, `encoding/json`, `errors`, `fmt`, `io`, `os`, `path/filepath`, `runtime/debug`, `slices`, `strconv`, `strings`, `sync`, `time`, `unicode/utf8`.

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,7 +6,9 @@ import (
"os"
"path/filepath"
"strings"
"unicode"
"git.golem15.com/golem15/summercms/internal/argmask"
"git.golem15.com/golem15/summercms/modules/sunscreen"
"github.com/charmbracelet/colorprofile"
)
@@ -70,7 +72,8 @@ func WithHint(err error, explanation, try string) error {
}
// ErrReported marks an error whose details the command already printed:
// the error block prints nothing and the exit code is 1.
// the error block prints nothing and the exit code is 1, or the code given
// with WithExitCode.
var ErrReported = errors.New("bonfire: error already reported")
type reportedError struct {
@@ -93,13 +96,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 +209,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 +218,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 +250,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,19 +285,20 @@ 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 " + argmask.Quote(rel) + "; {bin}" + tryArgs(args)
return &HintError{Err: err, Explanation: wrongDirectoryExplanation, Try: try}
}
// shellQuote single-quotes s when a shell would split or expand it.
func shellQuote(s string) string {
if s != "" && !strings.ContainsAny(s, " \t\n'\"\\$`*?[]{}()<>|&;#~!") {
return s
// tryArgs renders args for the wrong-directory Try line: empty for no
// args, otherwise a space and argmask.Join(args), which quotes each
// argument for a shell and masks the values of secret-named long flags
// (also a value that starts with "-"; only "--" is left alone), so a
// password typed on the command line does not reach stderr and CI logs.
func tryArgs(args []string) string {
if len(args) == 0 {
return ""
}
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
return " " + argmask.Join(args)
}
// parseUnknownCommand reads the command name and cobra's "Did you mean

View File

@@ -0,0 +1,331 @@
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, WR-11): 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 whatever the value's first character, and keeps every other
// argument; only a following "--" is not taken as the value.
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 ***"},
// pflag takes the next argument as the value of a non-boolean
// flag even when it starts with "-": "--force" is the token here.
{[]string{"sync", "--token", "--force"}, "cd ..; {bin} sync --token ***"},
{[]string{"admin:create", "--email", "a@b.c", "--password", "-Xk9!q"}, "cd ..; {bin} admin:create --email a@b.c --password ***"},
{[]string{"sync", "--client-secret", "-s3cr3t", "--verbose"}, "cd ..; {bin} sync --client-secret *** --verbose"},
{[]string{"sync", "--token", "--", "x"}, "cd ..; {bin} sync --token -- x"},
}
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", "-Xk9", "-s3cr3t"} {
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,10 +177,10 @@ 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)
out.Success(Sanitize(item))
}
return errors.Join(errs...)
}

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")
@@ -295,7 +310,7 @@ func (m processModel) View() tea.View {
if total > 0 {
pct = float64(m.done) / float64(total)
}
op := m.operation
op := Sanitize(m.operation)
if op == "" {
op = "Processing"
}
@@ -306,82 +321,226 @@ 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
// done is closed when the program's Run has returned; printAbove never
// sends to a program whose Run has ended, whatever ended it.
done <-chan struct{}
}
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 and only then builds the program; when
// another view holds the slot, runLive returns started false without
// building a program or 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.
//
// The program does not take ctx: when ctx is cancelled, a watcher ends
// the view with its last frame and unregisters it in the same step under
// liveMu, without waiting for work, so lines printed after the
// cancellation print plainly below the view instead of reaching a stopped
// program. A program whose Run ended on its own is unregistered as well
// and is never sent lines.
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},
// Claim the slot before building the program: a widget that loses it
// builds none, so it registers no child context on ctx and opens no
// TEA_TRACE file. NewProgram takes no bonfire lock and prints nothing.
finished := make(chan struct{})
liveMu.Lock()
if activeView != nil {
liveMu.Unlock()
return false, nil
}
prog := tea.NewProgram(model,
tea.WithInput(nil),
tea.WithOutput(out),
tea.WithoutSignalHandler(),
tea.WithContext(ctx),
tea.WithColorProfile(c.profile()),
)
finished := make(chan struct{})
view := &liveView{owner: c, prog: prog, done: finished}
activeView = view
liveMu.Unlock()
go func() {
defer close(finished)
_, _ = prog.Run()
}()
liveMu.Lock()
activeProg = prog
liveMu.Unlock()
stopWatch := watchContext(ctx, view, finished)
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
}
// When the context watcher already finished the program, its Run
// has returned: Send returns at once and finished is closed.
prog.Send(finishMsg{err: result})
<-finished
liveMu.Unlock()
stopWatch()
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.
// watchContext ends view when ctx is cancelled before the program
// finishes: under liveMu it unregisters the view, sends the finish message
// with ctx's error and waits for the program, the same single step as
// runLive's cleanup, so a printer either reaches the running program or
// waits and prints plainly after the last frame. When the program finishes
// first (normal end, a signal, Run failing), the view is only unregistered.
// The returned function waits for the watcher; call it after finished is
// closed and without holding liveMu.
func watchContext(ctx context.Context, view *liveView, finished <-chan struct{}) (wait func()) {
exited := make(chan struct{})
go func() {
defer close(exited)
select {
case <-ctx.Done():
liveMu.Lock()
if activeView == view {
activeView = nil
}
view.prog.Send(finishMsg{err: ctx.Err()})
<-finished
liveMu.Unlock()
case <-finished:
liveMu.Lock()
if activeView == view {
activeView = nil
}
liveMu.Unlock()
}
}()
return func() { <-exited }
}
// 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. A view
// whose program has ended (its context was cancelled, or Run returned on
// its own) is not running: printAbove unregisters it if it is still
// registered and reports false, so the caller prints s plainly.
func printAbove(owner *console, s string) bool {
liveMu.Lock()
defer liveMu.Unlock()
if activeProg == nil {
v := activeView
if v == nil || (owner != nil && v.owner != owner) {
return false
}
for _, line := range strings.Split(strings.TrimSuffix(s, "\n"), "\n") {
activeProg.Send(printAboveMsg(line))
select {
case <-v.done:
activeView = nil
return false
default:
}
// One message for the whole block, sent while holding liveMu: blocks
// from different goroutines keep the order of their calls.
v.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,714 @@
package bonfire
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"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, WR-14): an item name, an item error
// and the operation label carrying control sequences render without them,
// in the live view and on the plain path (-n on a terminal, forced color
// off a terminal).
func TestProcessViewStripsControl(t *testing.T) {
name := "evil\x1b]0;pwned\x07item"
var m tea.Model = newProcessModel(DefaultPalette(), "Push\x1b]0;pwned\x07ing", []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", "Push", "ing"} {
if !strings.Contains(ansi.Strip(out), want) {
t.Errorf("process view lacks %q:\n%q", want, out)
}
}
// The plain path: no live view, a colored writer that passes control
// sequences through (TestTableStripsControl).
for _, tc := range []struct {
name string
tty bool
globals Globals
}{
{"terminal with -n", true, Globals{Color: ColorAlways, NoInteraction: true}},
{"no terminal, color forced", false, Globals{Color: ColorAlways}},
} {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
c := newConsole(strings.NewReader(""), &buf, &buf, noEnv, boolPtr(tc.tty), boolPtr(tc.tty))
c.setGlobals(tc.globals)
if c.live() {
t.Fatal("the console runs live views; the plain path is not exercised")
}
err := Process(context.Background(), c, "Push\x1b]0;pwned\x07ing", []string{name}, func(context.Context, string) error { return nil })
if err != nil {
t.Fatal(err)
}
got := buf.String()
if bad := carriesInjected(got); bad != "" {
t.Errorf("plain Process line carries %q:\n%q", bad, got)
}
for _, want := range []string{"evil", "item"} {
if !strings.Contains(ansi.Strip(got), want) {
t.Errorf("plain Process line lacks %q:\n%q", want, got)
}
}
})
}
}
// countingCtx is a context that never ends and counts the children that
// context.WithCancel registers on it through AfterFunc and has not stopped.
type countingCtx struct {
mu sync.Mutex
outstanding int
done chan struct{}
}
func (c *countingCtx) Deadline() (time.Time, bool) { return time.Time{}, false }
func (c *countingCtx) Done() <-chan struct{} { return c.done }
func (c *countingCtx) Err() error { return nil }
func (c *countingCtx) Value(any) any { return nil }
func (c *countingCtx) AfterFunc(func()) (stop func() bool) {
c.mu.Lock()
c.outstanding++
c.mu.Unlock()
var once sync.Once
return func() bool {
stopped := false
once.Do(func() {
c.mu.Lock()
c.outstanding--
c.mu.Unlock()
stopped = true
})
return stopped
}
}
func (c *countingCtx) registered() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.outstanding
}
// TestNestedProcessBuildsNoProgram (IN-15): a Process nested in a running
// view loses the live slot before any program is built, so it registers no
// child context on the caller's context and opens no TEA_TRACE file; its
// items print as plain lines.
func TestNestedProcessBuildsNoProgram(t *testing.T) {
out := &lockedBuffer{}
c := newConsole(strings.NewReader(""), out, io.Discard, noEnv, boolPtr(true), boolPtr(true))
trace := t.TempDir() + "/trace.log"
ctx := &countingCtx{done: make(chan struct{})}
calls := 0
err := c.Spinner("outer", func() error {
t.Setenv("TEA_TRACE", trace)
return Process(ctx, c, "Nested", []string{"x"}, func(context.Context, string) error {
calls++
return nil
})
})
if err != nil {
t.Fatal(err)
}
if calls != 1 {
t.Errorf("fn ran %d times, want 1", calls)
}
if _, serr := os.Stat(trace); serr == nil {
t.Error("the nested Process built a program: the TEA_TRACE file exists")
}
if n := ctx.registered(); n != 0 {
t.Errorf("%d child contexts still registered on the caller's context, want 0", n)
}
if !strings.Contains(ansi.Strip(out.String()), symCheck+" x") {
t.Errorf("the item's success line is missing:\n%s", ansi.Strip(out.String()))
}
}
// TestPrintAboveAfterCancel (WR-09): cancelling the context given to
// Process ends its view and unregisters it without waiting for the work
// function; the lines printed afterwards through Println, Info and
// bonfire.PrintAbove (which reports false, so the caller prints the line
// itself, as diary does) are printed plainly once each, none lost.
func TestPrintAboveAfterCancel(t *testing.T) {
out := &lockedBuffer{}
c := newConsole(strings.NewReader(""), out, io.Discard, noEnv, boolPtr(true), boolPtr(true))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var got []string
err := Process(ctx, c, "Pushing", []string{"first", "second", "third"}, func(_ context.Context, item string) error {
got = append(got, item)
if item != "second" {
return nil
}
cancel()
registered := true
for deadline := time.Now().Add(2 * time.Second); time.Now().Before(deadline); time.Sleep(5 * time.Millisecond) {
liveMu.Lock()
registered = activeView != nil
liveMu.Unlock()
if !registered {
break
}
}
if registered {
t.Errorf("view still registered 2s after the cancellation")
}
c.Println("after-cancel-println")
c.Info("after-cancel-info")
if PrintAbove("after-cancel-printabove") {
t.Errorf("PrintAbove reported true after the cancellation")
} else {
_, _ = io.WriteString(out, "after-cancel-printabove\n")
}
return nil
})
if !errors.Is(err, context.Canceled) {
t.Errorf("Process returned %v, want context.Canceled", err)
}
for _, item := range got {
if item == "third" {
t.Error("fn ran for the item after the cancellation")
}
}
plain := ansi.Strip(out.String())
for _, line := range []string{"after-cancel-println", "after-cancel-info", "after-cancel-printabove"} {
if n := strings.Count(plain, line); n != 1 {
t.Errorf("%q printed %d times, want 1:\n%s", line, n, plain)
}
}
if PrintAbove("after") {
t.Error("a view is still registered after Process returned")
}
}

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

@@ -25,7 +25,7 @@ Use cases:
- Boot-time schema compilation: `cabana.CompileList` and `cabana.CompileForm` read a controller's YAML from the plugin's embedded tree, check that `modelClass` matches the controller's model name, and cache a locale-neutral schema. Each request gets a translated copy (`cabana.ListSchema.Localize`, `cabana.FormSchema.Localize`, `cabana.RelationSchema.Localize`) through [phrasebook](../phrasebook/README.md), with CLDR plural forms for the SPA's messages.
- Generic CRUD with `cabana.CRUDService`: list, show, create, update, delete and bulk delete. Writes run in transactions, and reads and writes are scoped by the controller's `pact.ListExtendQuery` and `pact.FormExtendQuery` hooks. `cabana.ExecuteList` applies search, sort, filters and pagination only on columns declared in the schema, so request parameters never reach SQL directly.
- Mass-assignment protection: writable form fields are bound to model columns at activation (`cabana.BindWritableFields`), and `cabana.ProjectWritableFields` drops unknown keys, case variants, nested objects and protected columns from request bodies; only a `type: repeater` field's list of rows is exempt from the nested-value drop. Declared `mltext`, `mltextarea` and `mlmarkdown` locale maps are lifted before that drop: the default locale becomes the host scalar and the other locales are written through a published `cabana.TranslationWriter` inside the same permissioned save transaction. Values are filled and validated through [lagoon](../lagoon/README.md); a value that does not fit its column (a `lagoon.FillTypeError`, such as a fraction for an integer field) is a 422 `validation_failed` on that field, and the form lifecycle hooks declared in `pact` (before and after create, update and delete) run around each write.
- Relations: `type: relation` form fields for belongsTo and belongsToMany (`cabana.FieldRelationProvider`, `cabana.FieldRelationContract`) with a paginated options endpoint and display labels in every record response; relation managers (`cabana.AdminRelationContractProvider`, `cabana.RelationContract`) served by `cabana.RelationService` for listing linked records and candidates, linking and unlinking, and creating related records. A contract is a belongsToMany (`cabana.RelationBelongsToMany`, the kind of a contract that leaves `Kind` empty) with a pivot model, or a hasMany (`cabana.RelationHasMany`) whose `ForeignKey` column on the related model points at the parent. A relation's child form comes from `manage.form` in `config_relation.yaml` (or a top-level `form`, WinterCMS's fallback), its read-only preview from `view.form` (same fallback), and a belongsToMany's pivot columns are edited through `pivot.form` (WinterCMS `pivot[x]` field names compile to `x`; pivot keys, timestamps and hook columns are refused); WinterCMS `$/<vendor>/<plugin>/...` paths resolve inside the same plugin only. A relation form accepts the scalar field types plus `datepicker` and `fileupload`; `relation`, `relation-manager`, `widget` and `partial` fail boot. The view panel's `toolbarButtons` (`create`, `update`, `delete`, `link`, `unlink`) are the capability of their routes. A relation's `messages` block takes the link keys (`link`, `linkHint`, `candidateSearch`, `linked`, `unlinkSelected`, `unlinkConfirm`, `unlinked`, `empty`) and the child and pivot modal keys (`create`, `createTitle`, `updateTitle`, `previewTitle`, `created`, `updated`, `deleteSelected`, `deleteConfirm`, `deleteOneConfirm`, `deleted`, `pivotTitle`, `pivotSaved`, `editPivot`, `createSubmit`, `updateSubmit`, `pivotSubmit`, `linkSubmit`), each defaulting to `backend::lang.messages.relation.*`. Framework code never guesses table, pivot or foreign-key names: the controller supplies them. A belongsTo field over a protected foreign key is read-only unless its contract sets `WritableForeignKey`; the protected key list itself does not change, and the submitted id is still rechecked through the scoped options query. A controller implementing `cabana.RelationLockProvider` returns a `cabana.RelationLock` per field and request: options and labels carry `locked: true` for those ids, and a create or update that adds or removes a locked id is answered 403 `forbidden` before any row is written.
- Relations: `type: relation` form fields for belongsTo and belongsToMany (`cabana.FieldRelationProvider`, `cabana.FieldRelationContract`) with a paginated options endpoint and display labels in every record response; relation managers (`cabana.AdminRelationContractProvider`, `cabana.RelationContract`) served by `cabana.RelationService` for listing linked records and candidates, linking and unlinking, and creating related records. A contract is a belongsToMany (`cabana.RelationBelongsToMany`, the kind of a contract that leaves `Kind` empty) with a pivot model, or a hasMany (`cabana.RelationHasMany`) whose `ForeignKey` column on the related model points at the parent. A relation's child form comes from `manage.form` in `config_relation.yaml` (or a top-level `form`, WinterCMS's fallback), its read-only preview from `view.form` (same fallback), and a belongsToMany's pivot columns are edited through `pivot.form` (WinterCMS `pivot[x]` field names compile to `x`; pivot keys, timestamps and hook columns are refused); WinterCMS `$/<vendor>/<plugin>/...` paths resolve inside the same plugin only. A relation form accepts the scalar field types plus `datepicker` and `fileupload`; `relation`, `relation-manager`, `widget` and `partial` fail boot. The view panel's `toolbarButtons` (`create`, `update`, `delete`, `link`, `unlink`) are the capability of their routes. A relation's `messages` block takes the link keys (`link`, `linkHint`, `candidateSearch`, `linked`, `unlinkSelected`, `unlinkConfirm`, `unlinked`, `empty`) and the child and pivot modal keys (`create`, `createTitle`, `updateTitle`, `previewTitle`, `created`, `updated`, `deleteSelected`, `deleteConfirm`, `deleteOneConfirm`, `deleted`, `pivotTitle`, `pivotSaved`, `editPivot`, `createSubmit`, `updateSubmit`, `pivotSubmit`, `linkSubmit`), each defaulting to `backend::lang.messages.relation.*`. Framework code never guesses table, pivot or foreign-key names: the controller supplies them. A belongsTo field over a protected foreign key is read-only unless its contract sets `WritableForeignKey`; the protected key list itself does not change, and the submitted id is still rechecked through the scoped options query. `ClearOnChange` on a belongsTo `cabana.FieldRelationContract` or a hasMany `cabana.RelationContract` names nullable columns (of the record, or of the related child) that are set to NULL whenever the relation changes the foreign key, so a value that only makes sense for one parent (a role in an organisation) never moves to another or outlives the membership; a form save that writes such a column through its own field keeps the submitted value. A controller implementing `cabana.RelationLockProvider` returns a `cabana.RelationLock` per field and request: options and labels carry `locked: true` for those ids, and a create or update that adds or removes a locked id is answered 403 `forbidden` before any row is written.
- Form widgets and controller actions: a `type: widget` field in `fields.yaml` names a plugin custom element (`widget:`, which must start with the owning plugin's `{vendor}-{plugin}-` prefix), the controller action it runs (`action:`, registered through `pact.HasAdminActions`) and the writable scalar fields of the same form the action may write back (`fill:`). The admin SPA posts the action to a cabana-owned route, so the CSRF check, permissions (the controller's plus the action's own) and record scoping (`pact.FormExtendQuery`) never depend on plugin code; the response carries only the declared fill keys whose values encode as JSON scalars (a value whose `MarshalJSON` writes an array or object, NaN or an infinity is dropped). Like the list schema's `toolbarActions`, the form schema carries a widget field only when the requesting administrator may run its action. The field's `context` applies to the action route as it does on save: a request without `record_id` is the create form's and one with it the update form's, and a widget its context hides on that form answers 404. Besides `record_id` and `values`, the request may carry `payload`, the widget's own JSON value (at most 64 KiB, a 422 above that; refused on toolbar and record routes), which reaches the action unfiltered as the `Payload` field of `pact.AdminActionInput`; the action may answer with `Data` on `pact.AdminActionResult`, written to the response as `data` exactly as it encodes (any JSON up to 256 KiB encoded, a 500 above that or when it cannot be encoded), not subject to the fill allowlist and omitted when nil. The element asks for its action with a `summer-action` event whose optional `detail.payload` becomes the request `payload`, and receives the answer as a `data` attribute plus a `summer-result` event with `{data, fill, message}`. Unknown keys, a foreign or invalid tag, an unregistered action or a fill key that is not a writable scalar field fail boot.
- Controller assets: a controller implementing `pact.AdminClientAssets` names JS (`.js`, `.mjs`) and CSS files under its plugin's `assets/` directory, Winter's `addJs`/`addCss`. They are read from the plugin's embedded tree at boot (a missing file fails boot; there is no disk override) and listed in the list and form schemas under `assets` as same-origin URLs with a `?v=` content hash. A form with a widget needs at least one JS file.
- Toolbar actions: `toolbar.buttons` in `config_list.yaml` lists the built-in `create` and `delete` next to names the controller registers through `pact.HasAdminActions`. Registered actions share one namespace with widget actions, `create` and `delete` are reserved, and each toolbar action needs a label. The list schema's `toolbarActions` carries only the actions the requesting administrator may run, with localized labels; an unknown name fails boot.
@@ -99,7 +99,7 @@ Guarded routes run surf's `locale.from-principal` after the backend guard: an in
| GET `/{vendor}/{plugin}/{controller}/singleton` | The id of a `pact.AdminSingleton` controller's one record, created when absent. |
| GET `.../fields/{field}/dependent-options` | Choices of a dropdown with `dependsOn` for the sibling value passed as `values[<sibling>]`, from the controller's `pact.DependentOptionsProvider`: `{applies, options: [{value, label, depth}]}` (`cabana.AdminDependentOptionsResult`). A field that is not a dependsOn dropdown answers 404. |
| GET `.../{id}/relations/{name}`, GET `.../{id}/relations/{name}/candidates` | Linked records and link candidates of a relation manager. |
| POST `.../{id}/relations/{name}/link`, POST `.../{id}/relations/{name}/unlink` | Link and unlink related records. A link body may carry `pivot` values for one id on a relation with a pivot form. On a hasMany, link sets and unlink clears the related record's foreign key. |
| POST `.../{id}/relations/{name}/link`, POST `.../{id}/relations/{name}/unlink` | Link and unlink related records. A link body may carry `pivot` values for one id on a relation with a pivot form. On a hasMany, link sets and unlink clears the related record's foreign key (and the contract's `ClearOnChange` columns). |
| POST `.../{id}/relations/{name}/records` | Create a related record through the relation's manage form and attach it to the record (a hasMany foreign key is set by the server; a belongsToMany gets its pivot row). Needs `create` in the view panel's `toolbarButtons` (403 otherwise). |
| GET and PUT `.../{id}/relations/{name}/records/{child}` | Show a child (needs `update` or a view form) and save it through the manage form (needs `update`). |
| POST `.../{id}/relations/{name}/delete` | `{ids}`: delete children through their model (needs `delete`). A belongsToMany record loses this record's pivot row first. All or nothing. |
@@ -269,10 +269,10 @@ func (p *Plugin) AdminFS() fs.FS { return adminFS }
| `cabana.SettingsMeta` | Meta of a settings result: `secrets` (field to `stored`, `env` or `empty`) and the richeditor `notices`; omitted when empty. |
| `cabana.SecretFallbacks` | Implemented by a settings model whose password fields fall back to a config key when the column is empty: field name to compass key. |
| `cabana.SecretStored` / `cabana.SecretEnv` / `cabana.SecretEmpty` | The states of a write-only settings secret. |
| `cabana.FieldRelationProvider` / `cabana.FieldRelationContract` | Controller-supplied bindings for `type: relation` form fields. `WritableForeignKey` on a belongsTo contract makes a protected foreign key writable through that field. |
| `cabana.FieldRelationProvider` / `cabana.FieldRelationContract` | Controller-supplied bindings for `type: relation` form fields. `WritableForeignKey` on a belongsTo contract makes a protected foreign key writable through that field; `ClearOnChange` lists nullable columns of the record set to NULL when a save changes the key, unless the save writes them. |
| `cabana.RelationLock` | The related ids of one relation field the requesting administrator may not add or remove, and the message of the 403. |
| `cabana.RelationLockProvider` | Controller capability that returns the `cabana.RelationLock` of a relation field per request; enforced on create and update. |
| `cabana.AdminRelationContractProvider` / `cabana.RelationContract` | Controller-supplied bindings for relation managers. |
| `cabana.AdminRelationContractProvider` / `cabana.RelationContract` | Controller-supplied bindings for relation managers. `ClearOnChange` on a hasMany lists nullable columns of the child set to NULL by link and unlink. |
| `cabana.RelationBelongsToMany` / `cabana.RelationHasMany` | The two `RelationContract.Kind` values; an empty `Kind` is a belongsToMany. |
| `cabana.AdminRelationChildCreate` / `cabana.AdminRelationChildShow` / `cabana.AdminRelationChildUpdate` / `cabana.AdminRelationChildDelete` / `cabana.AdminRelationPivotShow` / `cabana.AdminRelationPivotUpdate` | Swag annotations of the relation child and pivot routes. |
| `cabana.RelationMutationInput` | Body of the link and unlink routes: `IDs` and, for a link of one id, `Pivot` form values. |
@@ -387,6 +387,8 @@ The Phase 18 pieces (money field and column, write-only settings secrets, riched
The Phase 18.1 pieces (the taglist field and the flat reorder) are covered by Docker-free unit tests in `taglist_field_test.go` (TestTaglistFieldCompile, TestTaglistFieldValidate for the save check in both modes, TestTaglistFieldHydrate for the record GET) and `reorder_flat_test.go` (TestReorderFlatCompile, which also checks the localized list schema, and TestReorderFlatNodes for the save's node check), and by Postgres tests in `catalog_smoke_test.go` (TestTaglistFieldSmoke, TestReorderFlatSmoke with a partitioned subtest) and `reorder_test.go` (TestReorderPostgres for the tree reorder). The admin specs are `admin/tests/form/TaglistField.test.ts` and `admin/tests/views/ReorderView.test.ts`. `scripts/check-phase18.1.sh` runs them by name.
`ClearOnChange` is covered by `clear_on_change_field_test.go` (TestClearOnChangeFieldBoot, Docker-free, and TestClearOnChangeBelongsTo on Postgres: a changed, emptied, kept or co-submitted belongsTo key) and `clear_on_change_test.go` (TestClearOnChangeRelationBoot and, on Postgres, TestClearOnChangeHasMany: link, unlink and a deferred link committed by the parent's first save).
A Detached bulk action (`pact.AdminBulkAction.Detached`) is covered by the Postgres test TestDetachedBulkAction in `bulk_detached_test.go`, on the roster fixture with two test-only actions: Run sees no transaction (`TxFromContext`) and holds no row lock (another connection's `SELECT ... FOR UPDATE NOWAIT` succeeds during Run); the empty, absent and partial-selection rules match the locked path; a `ValidationError` from Run is 422 and the rows written before it stay written; the same action without Detached still runs in the transaction holding the row locks. `scripts/check-phase18.1.sh` requires it in its postgres stage.
The localization of refusals is covered by Docker-free unit tests in `localize_error_test.go` (TestLocalizeControllerErrorValidation, TestLocalizeControllerErrorForbidden and TestWriteCRUDErrorLocalized, on a two-locale catalog; the plugin's error value stays unchanged and text without `::` is never looked up) and by the Postgres tests TestValidationErrorLocalized (a form hook's `cabana.ValidationError` in en and pl) and TestReorderHookRefusalLocalized (both refusal types from `pact.ReorderAfterSave`).

View File

@@ -0,0 +1,222 @@
package cabana
import (
"context"
"strings"
"testing"
"testing/fstest"
)
// cocTeam is the related model of the ClearOnChange belongsTo fixture.
type cocTeam struct {
ID uint `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
}
func (cocTeam) TableName() string { return "cabana_coc_teams" }
// cocMember belongs to a team through the protected organisation_id; its
// role only makes sense inside that team.
type cocMember struct {
ID uint `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
OrganisationID *uint `gorm:"column:organisation_id"`
Role *string `gorm:"column:role"`
Note *string `gorm:"column:note"`
}
func (cocMember) TableName() string { return "cabana_coc_members" }
func (cocMember) Fillable() []string { return []string{"name", "role", "note"} }
func (cocMember) Rules() map[string]string { return map[string]string{"name": "required"} }
type cocController struct {
relations []FieldRelationContract
}
func (cocController) ID() string { return "acme.demo.members" }
func (cocController) ModelName() string { return "Member" }
func (cocController) ConfigDir() string { return "controllers/members" }
func (cocController) NewRecord() any { return &cocMember{} }
func (c cocController) AdminFieldRelations() []FieldRelationContract {
return c.relations
}
// cocTeamContract is the team field's contract with ClearOnChange role.
func cocTeamContract(clear ...string) FieldRelationContract {
return FieldRelationContract{
Field: "team", Kind: relationKindBelongsTo, NewRelated: func() any { return &cocTeam{} },
ForeignKey: "organisation_id", WritableForeignKey: true, ClearOnChange: clear,
}
}
func cocFS() fstest.MapFS {
return fstest.MapFS{
"controllers/members/config_list.yaml": &fstest.MapFile{Data: []byte("modelClass: Member\nlist: ~/plugins/acme/demo/models/member/columns.yaml\n")},
"controllers/members/config_form.yaml": &fstest.MapFile{Data: []byte("name: members\nform: ~/plugins/acme/demo/models/member/fields.yaml\nmodelClass: Member\n")},
"models/member/columns.yaml": &fstest.MapFile{Data: []byte("columns:\n name:\n label: Name\n")},
"models/member/fields.yaml": &fstest.MapFile{Data: []byte(`fields:
name:
label: Name
type: text
team:
label: Team
type: relation
nameFrom: name
role:
label: Role
type: text
note:
label: Note
type: text
`)},
}
}
func cocCompile(relations ...FieldRelationContract) (*CompiledController, error) {
reg, err := compileRegistry([]controllerRef{{plugin: formPlugin{fsys: cocFS()}, ctl: cocController{relations: relations}}})
if err != nil {
return nil, err
}
cc, _ := reg.Get("acme.demo.members")
return cc, nil
}
// TestClearOnChangeFieldBoot: ClearOnChange on a form relation field is
// refused at boot on a belongsToMany, and for a column the record lacks, a
// column that is not nullable, the foreign key and the primary key; the
// error names the controller, the field and the column.
func TestClearOnChangeFieldBoot(t *testing.T) {
if _, err := cocCompile(cocTeamContract("role", "note")); err != nil {
t.Fatalf("a valid ClearOnChange: %v", err)
}
many := cocTeamContract("role")
many.Kind, many.ForeignKey, many.WritableForeignKey = relationKindBelongsToMany, "", false
for name, tc := range map[string]struct {
contract FieldRelationContract
want string
}{
"belongsToMany": {many, "ClearOnChange is only valid on belongsTo"},
"missing column": {cocTeamContract("rank"), "ClearOnChange column rank is not a column of the model"},
"not nullable": {cocTeamContract("name"), "ClearOnChange column name is not nullable"},
"foreign key": {cocTeamContract("organisation_id"), "ClearOnChange column organisation_id is the foreign key"},
"primary key": {cocTeamContract("id"), "ClearOnChange column id is the primary key"},
} {
t.Run(name, func(t *testing.T) {
_, err := cocCompile(tc.contract)
if err == nil || !strings.Contains(err.Error(), tc.want) || !strings.Contains(err.Error(), "acme.demo.members") || !strings.Contains(err.Error(), "field team") {
t.Fatalf("err = %v, want %q naming the controller and the field", err, tc.want)
}
})
}
}
// TestClearOnChangeBelongsTo: a save that changes a belongsTo key clears the
// contract's ClearOnChange columns unless the same save writes them; a save
// that keeps the key, and a contract without ClearOnChange, leave them.
func TestClearOnChangeBelongsTo(t *testing.T) {
_, db := newListService(t)
if err := db.Migrator().DropTable(&cocMember{}, &cocTeam{}); err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&cocMember{}, &cocTeam{}); err != nil {
t.Fatal(err)
}
red, blue := cocTeam{Name: "Red"}, cocTeam{Name: "Blue"}
for _, team := range []*cocTeam{&red, &blue} {
if err := db.Create(team).Error; err != nil {
t.Fatal(err)
}
}
cc, err := cocCompile(cocTeamContract("role"))
if err != nil {
t.Fatal(err)
}
svc := CRUDService{DB: db}
ctx := context.Background()
str := func(s string) *string { return &s }
seed := func(t *testing.T, team *uint, role string) uint {
t.Helper()
m := cocMember{Name: "M", OrganisationID: team, Role: str(role), Note: str("note")}
if err := db.Create(&m).Error; err != nil {
t.Fatal(err)
}
return m.ID
}
load := func(t *testing.T, id uint) cocMember {
t.Helper()
var m cocMember
if err := db.Take(&m, id).Error; err != nil {
t.Fatal(err)
}
return m
}
update := func(t *testing.T, cc *CompiledController, id uint, body map[string]any) cocMember {
t.Helper()
body["name"] = "M"
if _, err := svc.Update(ctx, cc, id, RecordInput{Body: body}); err != nil {
t.Fatalf("update %v: %v", body, err)
}
return load(t, id)
}
roleIs := func(t *testing.T, m cocMember, want string) {
t.Helper()
switch {
case want == "" && m.Role != nil:
t.Fatalf("role = %q, want NULL", *m.Role)
case want != "" && (m.Role == nil || *m.Role != want):
t.Fatalf("role = %v, want %q", m.Role, want)
}
if m.Note == nil || *m.Note != "note" {
t.Fatalf("a column outside ClearOnChange changed: note = %v", m.Note)
}
}
t.Run("another record clears", func(t *testing.T) {
m := update(t, cc, seed(t, &red.ID, "owner"), map[string]any{"team": blue.ID})
if m.OrganisationID == nil || *m.OrganisationID != blue.ID {
t.Fatalf("organisation_id = %v", m.OrganisationID)
}
roleIs(t, m, "")
})
t.Run("none clears", func(t *testing.T) {
m := update(t, cc, seed(t, &red.ID, "owner"), map[string]any{"team": nil})
if m.OrganisationID != nil {
t.Fatalf("organisation_id = %v", *m.OrganisationID)
}
roleIs(t, m, "")
})
t.Run("the same key keeps", func(t *testing.T) {
roleIs(t, update(t, cc, seed(t, &red.ID, "owner"), map[string]any{"team": red.ID}), "owner")
})
t.Run("no relation value keeps", func(t *testing.T) {
roleIs(t, update(t, cc, seed(t, &red.ID, "owner"), map[string]any{}), "owner")
})
t.Run("a submitted column wins", func(t *testing.T) {
roleIs(t, update(t, cc, seed(t, &red.ID, "owner"), map[string]any{"team": blue.ID, "role": "member"}), "member")
})
t.Run("a create that sets the key", func(t *testing.T) {
rec, err := svc.Create(ctx, cc, RecordInput{Body: map[string]any{"name": "New", "team": red.ID, "note": "note"}})
if err != nil {
t.Fatal(err)
}
id, _ := asUint(rec["id"])
m := load(t, id)
if m.OrganisationID == nil || *m.OrganisationID != red.ID {
t.Fatalf("organisation_id = %v", m.OrganisationID)
}
roleIs(t, m, "")
rec, err = svc.Create(ctx, cc, RecordInput{Body: map[string]any{"name": "New", "team": red.ID, "role": "admin", "note": "note"}})
if err != nil {
t.Fatal(err)
}
id, _ = asUint(rec["id"])
roleIs(t, load(t, id), "admin")
})
t.Run("without ClearOnChange nothing is cleared", func(t *testing.T) {
plain, err := cocCompile(cocTeamContract())
if err != nil {
t.Fatal(err)
}
roleIs(t, update(t, plain, seed(t, &red.ID, "owner"), map[string]any{"team": blue.ID}), "owner")
})
}

View File

@@ -0,0 +1,119 @@
package cabana_test
import (
"net/http"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/cabana"
)
// clearOnChangeContracts is dfContracts with ClearOnChange columns on the
// parts hasMany.
func clearOnChangeContracts(columns ...string) func() []cabana.RelationContract {
return func() []cabana.RelationContract {
cs := dfContracts()
cs[1].ClearOnChange = columns
return cs
}
}
// TestClearOnChangeRelationBoot: ClearOnChange on a relation contract is
// refused at boot on a belongsToMany, and for a column the child lacks, a
// column that is not nullable, the ForeignKey and the primary key; the
// error names the controller, the relation and the column.
func TestClearOnChangeRelationBoot(t *testing.T) {
member := func() []cabana.RelationContract {
cs := dfContracts()
cs[0].ClearOnChange = []string{"email"}
return cs
}
for name, tc := range map[string]struct {
contracts func() []cabana.RelationContract
want string
}{
"belongsToMany": {member, "relation members: ClearOnChange is only valid on a hasMany contract"},
"missing column": {clearOnChangeContracts("rank"), "relation parts: ClearOnChange column rank is not a column of the model"},
"not nullable": {clearOnChangeContracts("label"), "relation parts: ClearOnChange column label is not nullable"},
"foreign key": {clearOnChangeContracts("gadget_id"), "relation parts: ClearOnChange column gadget_id is the foreign key"},
"primary key": {clearOnChangeContracts("id"), "relation parts: ClearOnChange column id is the primary key"},
} {
t.Run(name, func(t *testing.T) {
err := activateDeferred(t, withContracts(nil, tc.contracts)(t), nil)
if err == nil || !strings.Contains(err.Error(), tc.want) || !strings.Contains(err.Error(), "acme.deferred.gadgets") {
t.Fatalf("err = %v, want %q naming the controller", err, tc.want)
}
})
}
if err := activateDeferred(t, withContracts(nil, clearOnChangeContracts("due_on"))(t), nil); err != nil {
t.Fatalf("a valid ClearOnChange: %v", err)
}
}
// TestClearOnChangeHasMany: a hasMany link and unlink in the relation
// manager clear the contract's ClearOnChange columns of the child, a
// deferred link committed by the parent's first save too, and a contract
// without ClearOnChange leaves them.
func TestClearOnChangeHasMany(t *testing.T) {
env := newDeferredEnvWith(t, withContracts(nil, clearOnChangeContracts("due_on"))(t))
g := env.gadget(t, "g-"+env.stamp, false)
parts := func(gadget uint, rest string) string { return dfPath(gadget, "/relations/parts"+rest) }
dated := func(t *testing.T, owner uint, label string) uint {
t.Helper()
id := env.part(t, owner, label)
if err := env.db.Exec(`UPDATE cabana_deferred_parts SET due_on = '2026-11-01' WHERE id = ?`, id).Error; err != nil {
t.Fatal(err)
}
return id
}
dueOn := func(t *testing.T, id uint) bool {
t.Helper()
p, ok := env.partRow(t, id)
if !ok {
t.Fatalf("part %d is gone", id)
}
return p.DueOn != nil
}
t.Run("link clears", func(t *testing.T) {
free := dated(t, 0, "free")
want(t, "link", env.a.do(t, http.MethodPost, parts(g, "/link"), map[string]any{"ids": []uint{free}}, nil), http.StatusOK)
if p, _ := env.partRow(t, free); p.GadgetID == nil || *p.GadgetID != g || p.Label != "free" || dueOn(t, free) {
t.Fatalf("linked part %+v, want owned by %d with due_on NULL", p, g)
}
})
t.Run("unlink clears", func(t *testing.T) {
owned := dated(t, g, "owned")
want(t, "unlink", env.a.do(t, http.MethodPost, parts(g, "/unlink"), map[string]any{"ids": []uint{owned}}, nil), http.StatusOK)
if p, _ := env.partRow(t, owned); p.GadgetID != nil || dueOn(t, owned) {
t.Fatalf("unlinked part %+v, want a NULL key and due_on", p)
}
})
t.Run("deferred link clears on the parent's first save", func(t *testing.T) {
h := sk(newSessionKey(t))
free := dated(t, 0, "deferred")
want(t, "deferred link", env.a.do(t, http.MethodPost, parts(0, "/link"), map[string]any{"ids": []uint{free}}, h), http.StatusOK)
if !dueOn(t, free) {
t.Fatal("a deferred link wrote the child before the save")
}
saved := env.a.do(t, http.MethodPost, "/acme/deferred/gadgets", map[string]any{"name": "d-" + env.stamp}, h)
want(t, "save", saved, http.StatusCreated)
parent := dataID(t, saved.Body.Bytes())
if p, _ := env.partRow(t, free); p.GadgetID == nil || *p.GadgetID != parent || dueOn(t, free) {
t.Fatalf("committed part %+v, want owned by %d with due_on NULL", p, parent)
}
})
t.Run("without ClearOnChange nothing is cleared", func(t *testing.T) {
plain := newDeferredEnv(t)
pg := plain.gadget(t, "p-"+plain.stamp, false)
free := plain.part(t, 0, "plain")
if err := plain.db.Exec(`UPDATE cabana_deferred_parts SET due_on = '2026-11-01' WHERE id = ?`, free).Error; err != nil {
t.Fatal(err)
}
want(t, "link", plain.a.do(t, http.MethodPost, dfPath(pg, "/relations/parts/link"), map[string]any{"ids": []uint{free}}, nil), http.StatusOK)
want(t, "unlink", plain.a.do(t, http.MethodPost, dfPath(pg, "/relations/parts/unlink"), map[string]any{"ids": []uint{free}}, nil), http.StatusOK)
if p, _ := plain.partRow(t, free); p.DueOn == nil {
t.Fatalf("a contract without ClearOnChange cleared due_on: %+v", p)
}
})
}

View File

@@ -768,7 +768,8 @@ func (s CRUDService) save(ctx context.Context, cc *CompiledController, id any, i
}
// The Form hooks read what this save writes for plugin field types.
ctx = withFieldWrites(ctx, decoded.writes)
if err := lagoon.Fill(target, fillAllowed(cc, target, op), projected, false); err != nil {
allowed := fillAllowed(cc, target, op)
if err := lagoon.Fill(target, allowed, projected, false); err != nil {
// A value that does not fit its column is the admin's input,
// not a missing capability: answer it on the field.
var typed *lagoon.FillTypeError
@@ -819,7 +820,15 @@ func (s CRUDService) save(ctx context.Context, cc *CompiledController, id any, i
if err := checkRelationLocks(ctx, tx, cc, target, relations); err != nil {
return err
}
if err := assignBelongsTo(cc, target, relations); err != nil {
// A belongsTo whose key changes clears its ClearOnChange columns,
// except those this save writes from its own form fields.
written := map[string]bool{}
for _, key := range allowed {
if _, ok := projected[key]; ok {
written[key] = true
}
}
if err := assignBelongsTo(cc, target, relations, written); err != nil {
return err
}
if update {

View File

@@ -25,6 +25,8 @@ type Member struct {
// OrganisationID is a protected column: the form writes it only through
// the team relation field.
OrganisationID *uint `gorm:"column:organisation_id"`
// TeamRole only makes sense inside the member's team.
TeamRole *string `gorm:"column:team_role"`
}
// Team is what a member belongs to; Group is what a member is put into.
@@ -156,7 +158,8 @@ func (MembersController) AdminSetPermissionValues(_ context.Context, field strin
// AdminFieldRelations binds the form's two relation fields. organisation_id
// is a protected column, so the team field would be read-only; the contract
// opts in to writing it through this field.
// opts in to writing it through this field. A save that moves the member to
// another team (or to none) clears team_role, unless it writes a new one.
func (MembersController) AdminFieldRelations() []cabana.FieldRelationContract {
return []cabana.FieldRelationContract{{
Field: "team",
@@ -164,6 +167,7 @@ func (MembersController) AdminFieldRelations() []cabana.FieldRelationContract {
NewRelated: func() any { return &Team{} },
ForeignKey: "organisation_id",
WritableForeignKey: true,
ClearOnChange: []string{"team_role"},
}, {
Field: "groups",
Kind: "belongsToMany",

View File

@@ -55,6 +55,15 @@ type RelationContract struct {
Columns map[string]string
HookPivotColumns []string
ExcludedRelatedIDs func(parent any) ([]uint, error)
// ClearOnChange names nullable columns of the related child (hasMany
// only) that are set to NULL whenever this relation changes the child's
// ForeignKey: a link and an unlink in the relation manager, deferred ones
// included. Values that only make sense for one parent (a member's role
// in an organisation, for example) then never move to another parent or
// outlive the membership. Each column must be a pointer field of the
// related model and neither the ForeignKey nor the primary key. The zero
// value changes nothing.
ClearOnChange []string
}
// kind is the contract's normalized kind.
@@ -482,10 +491,16 @@ func validateRelationContract(ctl pact.AdminController, contract RelationContrac
if err := validatePivotContract(contract); err != nil {
return err
}
if len(contract.ClearOnChange) > 0 {
return fmt.Errorf("relation %s: ClearOnChange is only valid on a hasMany contract", contract.Name)
}
case RelationHasMany:
if err := validateHasManyContract(contract); err != nil {
return err
}
if err := validateClearOnChange(contract.NewRelated(), contract.ClearOnChange, contract.ForeignKey); err != nil {
return fmt.Errorf("relation %s: %w", contract.Name, err)
}
default:
return fmt.Errorf("relation %s has unknown kind %q (want %s or %s)", contract.Name, contract.Kind, RelationBelongsToMany, RelationHasMany)
}
@@ -578,6 +593,46 @@ func validateHasManyContract(contract RelationContract) error {
return nil
}
// validateClearOnChange checks the ClearOnChange columns of a contract
// against model: each must be a column of the model, held in a pointer
// field (a nullable column), and neither foreignKey nor the primary key.
func validateClearOnChange(model any, columns []string, foreignKey string) error {
pk := primaryColumn(model)
for _, col := range columns {
if !identifier(col) {
return fmt.Errorf("ClearOnChange column %q is not an identifier", col)
}
if col == foreignKey {
return fmt.Errorf("ClearOnChange column %s is the foreign key", col)
}
if col == pk {
return fmt.Errorf("ClearOnChange column %s is the primary key", col)
}
field, ok := structFieldByColumn(model, col)
if !ok {
return fmt.Errorf("ClearOnChange column %s is not a column of the model", col)
}
if field.Type.Kind() != reflect.Pointer {
return fmt.Errorf("ClearOnChange column %s is not nullable (it needs a pointer field)", col)
}
}
return nil
}
// clearColumns sets each column of model to NULL, except the columns in
// keep.
func clearColumns(model any, columns []string, keep map[string]bool) error {
for _, col := range columns {
if keep[col] {
continue
}
if err := setModelColumn(model, col, nil); err != nil {
return err
}
}
return nil
}
// relationDeferrable reports whether a relation can be managed before its
// parent is saved: a belongsToMany always (the pivot row is written on
// save), a hasMany only with a nullable ForeignKey (the child is inserted
@@ -945,6 +1000,11 @@ func (s RelationService) linkRelated(ctx context.Context, tx *gorm.DB, cc *Compi
if err := setModelColumn(related, cr.Contract.ForeignKey, ownerPK); err != nil {
return 0, lifecycleFailure(cc, err)
}
// A link always changes the key: it adopts only children whose
// key is NULL.
if err := clearColumns(related, cr.Contract.ClearOnChange, nil); err != nil {
return 0, lifecycleFailure(cc, err)
}
if err := tx.WithContext(ctx).Save(related).Error; err != nil {
return 0, lifecycleFailure(cc, err)
}
@@ -1245,6 +1305,9 @@ func (s RelationService) unlinkRelated(ctx context.Context, tx *gorm.DB, cc *Com
if err := setModelColumn(child, cr.Contract.ForeignKey, nil); err != nil {
return 0, lifecycleFailure(cc, err)
}
if err := clearColumns(child, cr.Contract.ClearOnChange, nil); err != nil {
return 0, lifecycleFailure(cc, err)
}
if err := tx.WithContext(ctx).Save(child).Error; err != nil {
return 0, lifecycleFailure(cc, err)
}

View File

@@ -42,6 +42,15 @@ type FieldRelationContract struct {
// WritableForeignKey declares a belongsTo foreign key that is a protected
// fill key writable through this relation field.
WritableForeignKey bool
// ClearOnChange names nullable columns of the record (belongsTo only)
// that are set to NULL whenever a save changes this field's foreign key
// (to another record or to none), unless the same save writes the column
// through its own form field. Values that only make sense for one parent
// (a member's role in an organisation, for example) then never move to
// another parent or outlive the membership. Each column must be a
// pointer field of the record and neither the ForeignKey nor the primary
// key. The zero value changes nothing.
ClearOnChange []string
}
// RelationLock names the related records of one relation field that the
@@ -245,10 +254,16 @@ func compileFieldRelation(ctl pact.AdminController, field FormField, contract Fi
}
out.Nullable = fk.Type.Kind() == reflect.Pointer
out.ReadOnly = protectedFillKey(contract.ForeignKey) && !contract.WritableForeignKey
if err := validateClearOnChange(parent, contract.ClearOnChange, contract.ForeignKey); err != nil {
return nil, err
}
case relationKindBelongsToMany:
if contract.WritableForeignKey {
return nil, errors.New("WritableForeignKey is only valid on belongsTo")
}
if len(contract.ClearOnChange) > 0 {
return nil, errors.New("ClearOnChange is only valid on belongsTo")
}
if contract.ForeignKey != "" {
return nil, errors.New("belongsToMany cannot declare a ForeignKey")
}
@@ -644,16 +659,26 @@ func checkRelationScope(ctx context.Context, tx *gorm.DB, cc *CompiledController
// assignBelongsTo writes validated belongsTo ids (or null) onto the parent's
// foreign key before the row write. Read-only keys never reach this point: a
// protected foreign key is written only when its contract declares
// WritableForeignKey, the same rule compileFieldRelation applies.
func assignBelongsTo(cc *CompiledController, model any, values []relationValue) error {
// WritableForeignKey, the same rule compileFieldRelation applies. When the
// submitted key differs from the record's current one, the contract's
// ClearOnChange columns are set to NULL, except those in written (the fill
// keys this save wrote from its own form fields).
func assignBelongsTo(cc *CompiledController, model any, values []relationValue, written map[string]bool) error {
for _, value := range values {
c := value.fr.Contract
if value.fr.Multiple || value.fr.ReadOnly || (protectedFillKey(c.ForeignKey) && !c.WritableForeignKey) {
continue
}
var id any
var next uint
if !value.null {
id = value.ids[0]
next = value.ids[0]
}
if len(c.ClearOnChange) > 0 && currentKey(model, c.ForeignKey) != next {
if err := clearColumns(model, c.ClearOnChange, written); err != nil {
return lifecycleFailure(cc, err)
}
}
if err := setModelColumn(model, value.fr.Contract.ForeignKey, id); err != nil {
return lifecycleFailure(cc, err)
@@ -662,6 +687,33 @@ func assignBelongsTo(cc *CompiledController, model any, values []relationValue)
return nil
}
// currentKey is the value of model's foreign key column; a nil pointer and
// zero both read as 0 (no related record).
func currentKey(model any, column string) uint {
v := reflect.ValueOf(model)
for v.Kind() == reflect.Pointer {
if v.IsNil() {
return 0
}
v = v.Elem()
}
field := fieldByColumn(v, column)
for field.IsValid() && field.Kind() == reflect.Pointer {
if field.IsNil() {
return 0
}
field = field.Elem()
}
if !field.IsValid() || !field.CanInterface() {
return 0
}
n, err := asUint(field.Interface())
if err != nil {
return 0
}
return n
}
// syncBelongsToMany replaces the parent's pivot rows in submitted order: an
// explicit delete, then one bulk insert (Phase 5 join-table contract).
func syncBelongsToMany(ctx context.Context, tx *gorm.DB, cc *CompiledController, model any, values []relationValue) error {

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()

Some files were not shown because too many files have changed in this diff Show More