Files
museion/docs/devlog/20261204-microblog-5xx-wording-token-settings-page_log.md
T
AlexKucera 6938826101 feat(microblog): add app token settings page and honest 5xx wording
The app token could only be entered in the publish sheet, and that
field came back only after a 401/403, so a saved token had no standing
way to be replaced or removed. The new Settings → Micro.blog page
writes the same `MicropubTokenStore` slot the sheet reads, with a
status row read back from the Keychain after every write so a locked
keychain fails visibly instead of claiming "stored". The sheet now
names the stored token a send carries when its field is hidden.

Micro.blog's errors arrive as HTML pages, never Micropub JSON, so
`decodeFailure` fell through to a bare `HTTP 503` that said nothing
about whose side it was on. Any 5xx without a JSON description now
reads "unavailable on their side — retry in a few minutes"; a
described 5xx keeps its description and a 4xx keeps the bare status.
One decode covers create, publish, q=config, and media uploads.

Docs: AGENTS.md bullets, CHANGELOG [Unreleased] Added/Fixed, and the
devlog for this task plus the 0.1.2 release log.
2026-12-04 15:06:00 +01:00

82 lines
12 KiB
Markdown

# Micro.blog: honest 5xx wording + a Settings page for the app token
> **Date:** 2026-12-04
> **Type:** generic
> **Reference:** unticketed — owner report in session ("publishing returns error 503; there is no place in settings to save the auth token")
## Goal
Two things the owner hit while publishing to Micro.blog. First, the publish sheet showed `Micro.blog said 503: HTTP 503` — find out whether the 503 was Museion's or Micro.blog's, and make the message say so. Second, the Micro.blog app token had no home in Settings: it was entered once in the publish sheet, the field came back only after a 401/403, and a saved token could not be replaced or removed anywhere in the app.
## What Was Done
- **Diagnosis first, no code.** Probed `https://micro.blog/micropub` tokenless at 14:01 CET: 25/25 POSTs → `400 Invalid request` (text/html — their no-token answer), homepage 200, media endpoint 400, an app-like User-Agent 400. The request shape matched the current `microblog.dev/api/posting` docs (JSON create with `post-status: draft`; `update`/`replace` is JSON-only, which is what `Micropub.swift` sends). Their documented error vocabulary is 400 (bad request) and 401/403 (auth) — a 503 is their nginx front with the app behind it not answering, or a rate limit. Their news feed logs the same pattern repeatedly (Nov 25 "traffic spikes or attacks", Nov 30 "blog publishing stuck", 2025-04-26 "sorry for the 503s"). The bare `HTTP 503` in the sheet was `decodeFailure` falling through: the body was an HTML page, so the JSON decode found no `error_description`.
- `Micropub.decodeFailure(status:body:)` — the ONE failure decode every step shares (create, publish, q=config, media upload) — answers `unavailable on their side — retry in a few minutes (status.micro.blog)` for a 5xx that carries no JSON description. A described 5xx keeps its description; a 4xx keeps the bare `HTTP n`; 401/403 → `authFailed` unchanged. The sheet needed no change — it already renders `Micro.blog said <status>: <detail>`.
- `SettingsPage.microblog` (label **Micro.blog**, last in the sidebar after Provider Keys) + `MicroblogSettingsPage` in `SettingsPagesView.swift`: a status line (stored / not saved), a `SecureField` + **Save** (replaces the stored token), a **Remove** button while one is stored. It writes the same `MicropubTokenStore` slot the publish sheet reads, so the sheet picks the token up unchanged.
- `saved` is read back from the Keychain after every write: `save` is void (the `SecItemAdd` result is ignored), so a locked keychain would otherwise flip the status to "stored" falsely. On Save the read-back is compared with the token just written — an older token surviving a failed replace must not read as success.
- Pin in `MicropubTests`: `a 5xx names micro.blog's outage not a bare status` — 503 over an HTML body → the their-side wording; 422 over the same body → `HTTP 422`; 502 with `error_description` → the description.
- DOX: `Museion/AGENTS.md` (the Micropub bullet carries the 5xx contract and the token's two entry points; the Settings bullet names the page), the sheet's and both Settings files' headers, `CHANGELOG.md` `[Unreleased]` (Added + Fixed).
**Verification:** `./scripts/full-test.sh` — **736 executed / 736 passed / 0 failed** (run dir `_build/full-test/20261204T131024Z-Museion`; was 735, exactly +1). The filtered `MicropubTests` run 14/14 with `SwiftCompile` lines for every edited source. The new pin flip-verified: guard off (`status < 0`) → 14 executed / 13 passed / 1 failed, the one red being that pin with `detail → "HTTP 503"`; tree restored byte-exact (sha256 `21e9f101…` before and after). `swiftformat .` 0/153 formatted, `swiftlint .` exit 0.
**Follow-up, same session (owner: "there should be an indication on if the token was saved / is currently saved").** The owner was on the 14:59 build, so they had the page — the status was there, but as a muted intro-line text swap, which reads as description, not status. And the publish sheet showed nothing at all when a stored token was in use (the field just wasn't there).
- `MicroblogSettingsPage`: the intro is static again ("Where the Micro.blog app token lives."), and the status moved under the field row as its own row in the Settings verdict idiom — `✓ Token saved in your Keychain` in accent mono 11, `No token saved — publishing will ask for one` muted. Live: Save and Remove already read the Keychain back into `saved`.
- `MicropubPublishSheet`: when the field is hidden because a token is stored, the sheet says `Sends with the app token in your Keychain — Settings → Micro.blog manages it.`
- Verification: rail again on the settled tree — **736 / 736 / 0** (run dir `20261204T140402Z-Museion`). The two files were compiled by the standalone build that preceded the rail (`SwiftCompile` 2 lines each in its log; the rail's own build found them up to date — 0 lines, legitimately), so the artifact was checked instead: the dylib (15:04:07) postdates every source and carries all four new strings. `swiftformat --lint` 0/2, `swiftlint` 0. No new pin — both are view-only over the already-pinned store.
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| The 5xx wording lives in `decodeFailure`, not the sheet's `.requestFailed` branch | One decode serves create, publish, q=config, and media upload; every one of them surfaces through the same refusal line. Patching the sheet would have left the three other callers saying `HTTP 503`. |
| A JSON description on a 5xx still wins over the outage wording | Never hide a real server explanation behind the generic line. Their documented errors are 400-shaped JSON; a 5xx from the app itself might still carry one. |
| No retry, no `Retry-After` parsing | The user asked for honest wording. A 503 from an overloaded service is best retried by a human a few minutes later; automatic retries on a publish path can double-post. Add when a real outage pattern justifies it. |
| The page goes into `SettingsPagesView.swift`, not a new file | This repo disables `file_length`/`type_body_length` ("deliberately large cohesive units"); the surface pages and Provider Keys already live there. |
| Sidebar position: last, after Provider Keys | Enum declaration order is sidebar order. Credentials cluster at the end: provider keys (`.env`), then the one Keychain credential. |
| `saved` is read back from the Keychain, compared with the written token on Save | `MicropubTokenStore.save` ignores the `SecItemAdd` status. A void write plus an assumed `saved = true` would lie on a locked keychain; comparing against the new token also catches a failed replace that leaves the old one in place. |
| Capture `trimmedToken` into a local before clearing the field | `trimmedToken` is computed over `tokenField`. Clearing first made the read-back compare against `""` — always false. Caught before the first build. |
| No "Test token" button on the page | The publish sheet's Send as Draft is the test, and a draft creates nothing public. A read-only `q=config` probe is a fine follow-up if the owner wants it. |
| No view-layer pin | Repo convention: pure seams pinned, SwiftUI dogfooded. The page's logic is three calls into the store the `token store round-trips` pin already covers. |
| The owner's Keychain token was confirmed present but never read | Credential rule. A read-only `q=config` with it was offered and not run; the tokenless probe was enough to rule out a hard outage. |
| Unticketed, generic log | The owner asked for both fixes directly in session; no Gitea issue exists. The CHANGELOG carries the entry. |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| My first test patch called `outcomeDetail(outage)`, a helper that does not exist | Wrote the pin from a mental shape instead of the suite's | Read the landed region before building; replaced with the suite's `guard case let .requestFailed` shape |
| The patch tool's diffs around closing-brace runs read ambiguously (the `}` lines seemed added/re-indented) | Repeated `}` lines let the diff algorithm pair them unpredictably; the display is not the file | Read the landed regions and counted braces (open == close in every touched file) instead of trusting the diff |
| The rail log showed 0 `SwiftCompile` lines for four of the edited sources on first grep | The earlier filtered run had compiled them (2 lines each in its log); the rail recompiled only the sheet, whose header comment changed after that build | Verified the artifact instead: the dylib postdates every source and carries all five distinctive strings |
| `cut -c` on Swift source with em-dashes → `Illegal byte sequence` | macOS `cut` is not UTF-8-aware under the default locale | `read_file` for the region |
| A tokenless POST answers **400** `Invalid request` (text/html), not 401 | Micro.blog's no-token answer is not REST-shaped | Useful probe signature for "is the endpoint up": 400 means healthy, 503 means theirs |
| `MicropubPublishSheet` header said the token is "entered here ONCE" | True until this session | Header now names both entry points; the sheet's own logic is untouched |
| Owner follow-up: "there should be an indication" although the page had a status text | The status sat in the intro slot — a muted 12pt subtitle swapping text — which reads as description, not state; the sheet showed nothing when a stored token was in use | A dedicated status row under the field in the Settings verdict idiom (`✓ …` accent / muted); the sheet names the stored token it sends with |
| The first status-row patch nested `private var status` inside `form`'s body | The fuzzy patch matcher re-indents a replaced region that starts on a closing-brace run to the anchor's scope (the code-editing skill's rule 7) | Repaired by a programmatic splice of the whole struct between two unique doc-comment markers; verified column-0 struct line, brace balance, then swiftformat/swiftlint/build |
## Files Changed
| File | Change Summary |
|------|---------------|
| `Museion/Micropub.swift` | `decodeFailure`: 5xx → their-side wording (JSON description wins); doc comment |
| `Museion/SettingsPaneView.swift` | `SettingsPage.microblog` + label + detail switch; header |
| `Museion/SettingsPagesView.swift` | `MicroblogSettingsPage` (status row / Save / Remove, Keychain read-back); header |
| `Museion/MicropubPublishSheet.swift` | header comment (names the Settings manager); names the stored token a send carries when the field is hidden |
| `MuseionTests/MicropubTests.swift` | +1 pin (5xx wording; 4xx and described-5xx contrasts) |
| `Museion/AGENTS.md` | Micropub bullet (5xx contract, token entry points); Settings bullet (the page) |
| `CHANGELOG.md` | `[Unreleased]` → Added (the page), Fixed (the wording) |
| `docs/devlog/INDEX.md`, `AGENTS.md` | this log's row + the rolling window |
## Open Items & Next Steps
- [ ] Retry the publish that 503'd — the endpoint was healthy at 14:01 CET; if it 503s again, run a read-only `q=config` with the saved token (owner's go needed) to tell an account-specific 503 from a blip.
- [ ] Dogfood Settings → Micro.blog: the `✓ Token saved in your Keychain` row shows on open; Save replaces it (Remove appears); Remove flips it to `No token saved` and the publish sheet offers its field again; the sheet shows `Sends with the app token in your Keychain…` while one is stored.
- [ ] Optional: a **Test token** button (read-only `q=config`) on the page.
- [ ] Optional: `Retry-After`-aware outage wording; automatic retry on 503 for the draft send (mind double-posting).
- [ ] Pre-existing, untouched: the sheet's `showsTokenField` reads the Keychain on every body evaluation; a `.onAppear`-seeded state would read once.
- [ ] Commit (maintainer) — nothing was committed, pushed, or commented on the forge.
---
*Log written by write-log skill*