Skip to content

Phase 4: Account kinds: person, service, test - #1290

Merged
neuromechanist merged 19 commits into
feature/issue-1272-epic-orcid-cli-signinfrom
feature/issue-1284-phase4-account-kinds
Sep 8, 2026
Merged

neuromechanist merged 19 commits into
feature/issue-1272-epic-orcid-cli-signinfrom
feature/issue-1284-phase4-account-kinds

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Explicit account kinds (epic #1272, phase 4; ADR 0048). users.account_kind is person by default, service for automation, and test for a human's secondary persona. Kinds are set only by an owner, never inferred. Both non-person kinds are exempt from the ORCID gap (the interim admin-or-owner role exemption from ADR 0045 is gone; a person who is an admin is no longer exempt, and the operational admin accounts become service in the migration), refused by the device flow and by self-service key minting with the one typed reason service_account, and keyed only by an owner through nemar admin keys. A test account on production may create only xx sandbox datasets, enforced first in the real-dataset create gate.

Migration 0082 adds the column with a CHECK and sets the operational accounts by username (never by id): nemarOwner, nemarAdmin, test-admin, test-owner become service; cool-vibers, test-user, test-pending, test-verified, test-revoked become test; test-web stays a person because it is the shared web-QA account that must reach the authorize page and the Settings key form. The dev seed sets the same kinds.

New routes (owner only): POST /admin/users/:username/kind, and GET, POST, DELETE under /admin/users/:username/keys (mint refuses a person target). GET /admin/users?kind=. CLI: nemar admin kind <username> <kind>, nemar admin keys create|list|revoke, nemar admin users --kind, a Kind: line in listings and in nemar auth status when the kind is not person. The device flow reads one account row for lookup, confirm, and the token diagnosis.

Closes #1284
Part of epic #1272

Test plan

  • backend/test/account-kind-migration.test.ts: CHECK, default, the username-keyed data half against seeded rows, idempotent on re-run
  • test/profile-gaps-matrix.test.ts, backend/test/profile-gaps-route.test.ts: exemption by kind on all readers; a person admin is not exempt; an unrecognised role no longer matters
  • backend/test/device-auth-routes.test.ts, backend/test/api-keys-routes.test.ts: lookup, confirm, and collect-time refusals for both kinds; deny still allowed; self-mint refused
  • backend/test/admin-keys-routes.test.ts, backend/test/admin-kind-route.test.ts: owner only; person target refused; cap; list and revoke; audit rows; self, same-kind and verified-ORCID refusals on the kind route; the kind list filter
  • backend/test/dataset-create-test-kind.test.ts, backend/test/upload-gate.unit.test.ts: production real-dataset create refused for a test persona, sandbox allowed, non-production unrestricted
  • test/admin-keys-cli.test.ts, test/admin-kind-cli.test.ts, the admin users and auth status CLI tests; route inventory and export-surface pins
  • bun run lint, bun run typecheck, bun run migrations:d1-check (84 migrations replayed on real local D1); full bun test at the known baseline
  • Prove-the-test-fails: the person predicate in the device mint, the kind branch in the refusal, the matrix exemption, the production gate, the owner guard on the mint route, the self-refusal on the kind route
  • test/admin-owner-key-mint-flow.test.ts (live tier) probe-skips until the epic reaches dev; the owner path needs an optional TEST_OWNER_API_KEY
  • Before merge: confirm the exact production spellings of the four operational usernames with a read-only D1 query (see the PR discussion)

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1290

Version: 0.9.17-PR1290.d0c0378
Commit: d0c0378


🤖 Updated on each push.

POST /admin/users/:username/kind now answers { error, message } for all
four refusals (same_kind, orcid_linked, own_account,
kind_changed_concurrently) instead of an ad hoc code, so a caller reads
one shape everywhere. orcid_linked's message names `nemar auth profile
orcid unlink` directly. client.ts's prefersMessage learns the new codes
so the CLI surfaces the server's sentence, not a generic fallback.
KIND_CHANGE_GUARDED_UPDATE_SQL moves to module scope so its guarded
UPDATE can be exercised directly at the SQL level (the real-engine
substitute for a concurrent race bun:sqlite's single-writer harness
cannot reliably produce -- documented in this file's header after a
20x Promise.all experiment came back 200/200 every time).

Epic #1272 phase 4, #1284 review; ADR 0048.
- nemar admin users --kind now re-filters the listing client-side, so
  a backend that still ignores ?kind= cannot mislabel it (mirrors the
  --awaiting-approval / --no-upload-access pattern).
- nemar admin doctor kinds: a new, read-only check that every
  operational account (OPERATIONAL_ACCOUNT_KINDS, exported from
  shared/contract/user.ts so the migration test, the seed script's
  test, and this check stop drifting independently) still carries its
  expected kind; migration 0082's header now notes that the SQL copy
  of this list is separate from the one TypeScript source.
- nemar admin keys create appends a retry-safety hint on a network
  failure (statusCode === 0): the key may have minted before the
  connection dropped.
- --kind example added to `admin users --help`.
- test/api-export-surface.unit.test.ts: pin the new
  getAdminUserByUsername export.

Epic #1272 phase 4, #1284 review; ADR 0048.
webSessionMiddleware's catch block now tells a schema-shaped failure
(no such column/table -- a migration that has not landed on this
environment yet) apart from a plain transient lookup failure, each
with its own console.error prefix ([web-session-mw] SCHEMA vs
[web-session-mw]), so the two stop being indistinguishable in logs.

Epic #1272 phase 4, #1284 review; ADR 0048.
- DeviceAuthAccountRow.account_kind (and AdminKeyTargetRow through it),
  realDatasetCreateGate's input, and upload.ts's inline SELECT row all
  narrow from a bare string to the closed AccountKind type.
  identity_conflict narrows from number | boolean to number: every
  construction of DeviceAuthAccountRow is a raw D1 read, which is
  always a number.
- ProfileGapAccount.account_kind drops its | string escape hatch for
  the same reason; the one test that deliberately fed a bogus "member"
  value now casts it explicitly, with a comment explaining the cast
  defeats the type on purpose to prove the exemption still fails
  closed.
- applyServerUser's account_kind param and Config.accountKind both
  reuse AccountKind (via ContractUser / accountKindSchema) instead of
  a bare string.
- livenessStatusRefusal factors the pending/inactive-status prologue
  accountRefusal and accountLivenessRefusal both start with into one
  helper, so their shared ordering is structural rather than two
  functions kept in step by hand. The null/deleted check stays
  duplicated in each caller: it is what lets TypeScript narrow the row
  to non-null before the shared helper runs.

Epic #1272 phase 4, #1284 review; ADR 0048.
Two factual corrections from #1284 review:

- ADR 0048 and backend/test/upload-gate.unit.test.ts both claimed a
  service account "cannot reach [the dataset-create] route at all", on
  the theory that the device/key gates stop it. False: those gates
  only stop a service/test account from SELF-serving a key
  (self-minting, or the device flow's own sign-in). A key an owner
  minted for it through POST /admin/users/:username/keys authenticates
  through authMiddleware exactly like any other -- it never reads
  account_kind -- and reaches the dataset-create route, and every
  other authenticated route, normally. The ADR now explains that a
  service account is unrestricted at realDatasetCreateGate on purpose:
  that is what an operational/automation account is for.
- The claim that yahya/arnodelorme "already hold verified iDs, so
  nothing regresses" is qualified as "at the time of writing" and
  noted as an observation about the current catalog, not something
  the migration or the kind exemption depends on.

ADR 0048.
- The too_many_keys test name hardcoded "25"; it now interpolates
  MAX_LIVE_API_KEYS so the name cannot drift from the real cap.
- DELETE /admin/users/:username/keys/:id had a person-kind case only
  in the GET describe block -- every DELETE case seeded a service
  target, despite the file's own docstring promising DELETE works for
  any kind. Added the missing person-kind revoke case.

#1284 review; ADR 0048.
Two tests that could not previously fail for the reason they claimed
to test:

- "a tombstoned row is untouched" seeded a username the migration's IN
  (...) list never named ("nemarAdmin-deleted"), so the deleted_at
  gate it claimed to cover was never exercised. Now seeds the row
  under the EXACT username the migration lists ("nemarAdmin") with
  deleted_at stamped, in its own fresh database (migration 0080's
  case-insensitive unique index on username would otherwise collide
  with the shared fixture's live row of the same name).
- "case-insensitively" never actually seeded an inverted-case
  username. Now seeds "NEMARADMIN" in its own fresh database and
  proves the COLLATE NOCASE match.

Usernames are now derived from OPERATIONAL_ACCOUNT_KINDS (shared/
contract/user.ts) instead of a hand-copied list, so this file and the
migration's own username list can no longer drift from each other
independently of the SQL.

#1284 review; ADR 0048.
scripts/seed-dev-db.sql's account_kind UPDATEs had no test: nothing
proved the SQL text itself, as opposed to a hand-retyped copy of it,
assigns the right kind to the right usernames. ACCOUNT-KIND-SEED
marker comments let the new test slice the real statements out of the
file with readFileSync and execute them verbatim against a real
bun:sqlite database, rather than retyping them.

#1284 review; ADR 0048.
profile-gaps-matrix.test.ts and profile-gaps-route.test.ts proved the
kind exemption on GET /users/me, GET /auth/me, and the upload-access
refusal, but two other readers of the same account_kind column --
POST /auth/code/verify (builds its row inline) and PATCH /auth/profile
via fetchPublicUserById -- had no coverage at all. Both name
account_kind in their own hand-written SELECT, so nothing forced them
to agree with the SELECT the already-tested routes use.

#1284 review; ADR 0048.
The kind-change-between-confirm-and-collect diagnosis test only
exercised a change to 'service'. ADR 0048 has accountRefusal answer
the same service_account code for either non-person kind; this adds
the 'test' variant as proof the diagnosis path was not accidentally
written to special-case only 'service'.

#1284 review; ADR 0048.
@neuromechanist
neuromechanist merged commit 48c5764 into feature/issue-1272-epic-orcid-cli-signin Sep 8, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1284-phase4-account-kinds branch September 8, 2026 13:25

This branch was previously deployed

1 inactive deployment
npm — d0c03781 Deployed Sep 8, 2026 by neuromechanist via publish #2356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant