Repository navigation
Commit 98ba756
feat(auth): an AI service account is a sys_user row of kind agent that never signs in (#31779)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9H3PTJ3DC1WnJJ1S3UcQwgg)_
Part of #31739
Clause-②: yes
Metadata-protocol: none
Clause-② measured: the published face widens by two `sys_user` columns
(`principal_kind`, `accountable_user_id`), one `ReconcileOutcome` member
(`agent-account`) and one new plugin-auth module
(`service-account-identity.ts`). No authorable metadata key changes, so
the changeset level is `patch`.
## 维护者速读
- 改了什么:`sys_user` 新增「主体类型」与「负责人」两列,AI 服务账号是一条 `agent`
类型的用户记录;它不能登录、不能被模拟登录、不能持有密码或第三方登录绑定,成员关系自动绑定也跳过它。
- 为什么改:ADR-0151 第一步(D1 记录、D2 不登录),PR-2 的权限上限和 PR-3 的注册与密钥入口都建在这条记录上。
- 风险与回滚:每次建会话多读一次本人的 `sys_user` 行;读失败时仍拒绝(宁拒勿放),但返回
503「用户记录读取失败,请重试」并记录原因,不会把人误报成 AI 服务账号。现有用户都按「人」处理,行为不变。回滚即还原本 PR。
-
先看:`packages/plugins/plugin-auth/src/service-account-identity.ts`(拒绝与行守卫,留意
catch
分支)、`auth-manager-database-hooks.ts`(会话与凭证钩子的接入点)、`reconcile-membership.ts`。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
src/service-account-sign-in-refusal.test.ts
src/service-account-identity.test.ts`,应见 32 个用例全绿。
- 需要你做的:无,席位落地。
## What changes
- **D1, the row.** `sys_user` gains `principal_kind` (`human` default,
or `agent`) and `accountable_user_id` (a lookup to `sys_user`). Both are
`readonly` and listed in the D7 managed-extension set, so the generic
data path never writes them.
- **D1 rules, every writer including the system.** A new `sys_user` row
guard (priority 4, before the identity write guard) holds three rules. A
user's kind is set at creation and never changes. An `agent` row names
an existing person as its owner, never itself and never another account.
A person names no owner. Refusals are `400 VALIDATION_FAILED` with
ADR-0114 field codes (`required`, `reference_not_found`,
`invalid_value`).
- **D2, no session.** `session.create.before` first refuses an `agent`
user with `403 PERMISSION_DENIED`. better-auth routes every session
through `internalAdapter.createSession`, so this covers password, magic
link, one-time code, passkey, social and SSO sign-in, impersonation and
the server-side principal-session mint.
- **D2, no credential.** `account.create.before` refuses a `sys_account`
row (a password or a provider link) for an `agent` user.
- **A failed read of the user row.** Both refusals fail closed, but as
an outage: `503 SERVICE_UNAVAILABLE` with "the user record could not be
read … try again", and the cause logged once (class and message, never
the session or credential). A person never sees the AI service account
refusal because the database blinked.
- **Membership.** `reconcileMembership` answers the new outcome
`agent-account` and binds nothing. The one-time backfill skips `agent`
rows.
- **Translations.** The two new columns are translated in all ten
locales; `en` is regenerated.
Nothing registers an AI service account yet. The agent ceiling and key
admission (PR-2) and the registration, key and lifecycle doors (PR-3)
follow, each from a fresh main.
## Tests
New, 32 cases:
- `service-account-identity.test.ts` (19, real ObjectQL + SQLite): born
with its kind, kind immutable for system, multi-row and user-context
writes, the owner rules, the reconciler and backfill exemption, and
CONTROLs for people.
- `service-account-sign-in-refusal.test.ts` (13, AuthManager on the
shared memory engine): password, magic link, impersonation,
principal-session mint, `internalAdapter.createSession`,
`createAccount`/`linkAccount` refused with no row written; a failed row
read refuses both as a 503 read fault with the cause logged; CONTROLs
where a person signs in, is impersonated and links a provider as before.
Forced test edits, all in `first-session-membership-ordering.test.ts`,
because the session hook now reads the user's own row first:
- `a no-bind sign-in costs NO extra membership read — the re-read is
gated on the outcome`: `find` was "never called"; it is now "called
exactly once, with the `sys_user` principal-kind read". The membership
read count is unchanged.
- `a sign-in that created nobody costs no read beyond the original
selection`: the same edit.
- `a broken engine never breaks session create`: the double's `find` now
answers the `sys_user` read and still fails every membership read. A
failed principal-kind read refuses the session on purpose, pinned in the
new sign-in refusal file.
## Acceptance notes
- PR-1 of the approved three-PR serial split (D1+D2). The card stays
open.
- The same-organisation rule for the owner is left to the PR-3
registration door, which knows the organisation.
- ADR-0151's status line still reads "Proposed" although it merged;
flagged to the seat, not touched here.
- Seat: CONTRACT_REVIEW_TIER review owed on this PR.
## Verification
<details><summary>Tests and typecheck</summary>
- `pnpm --filter @objectstack/plugin-auth test` (lock): 248 files
passed, 4438 passed / 10 skipped, at 9a03a66 and again at d2d101d.
- `pnpm --filter @objectstack/platform-objects test`: 120 files, 1931
passed.
- `tsc --noEmit` for plugin-auth and platform-objects: exit 0.
- `eslint packages/plugins/plugin-auth packages/platform-objects
--no-inline-config`: exit 0.
- `check-i18n-bundles`: all bundles in sync.
- After the read-fault change: `check:dispatcher-error-vocabulary`,
`check:error-status-conformance`, `check:error-code-casing` and
plugin-auth eslint exit 0.
- After merging main (9a03a66): the three touched test files, 52
passed; full `pnpm build` 119/119.
</details>
<details><summary>Ablations (each red, then restored: blob == HEAD, `git
diff HEAD` empty)</summary>
Each through `scripts/ablation-replace.mjs`; the named pin went red and
the file came back with blob == HEAD.
1. Session refusal removed from `session.create.before`: password, magic
link, impersonation, mint and createSession pins red.
2. Credential refusal removed from `account.create.before`:
createAccount/linkAccount pin red.
3. Kind-change check removed from the row guard: immutability pins red.
4. Owner-required check removed: owner pins red.
5. Reconciler exemption removed: reconciler pin red.
6. Backfill skip removed: backfill pin red.
7. Person-with-owner check removed: person-owner pin red.
8. Session refusal fails open on a read error: "a session whose user row
cannot be read is refused" red.
9. Credential refusal fails open on a read error: "a credential whose
user row cannot be read is refused" red.
After seat review (read-fault refusal, d2d101d):
10. Session read fault answers the AI service account refusal instead:
"a session whose user row cannot be read is refused as a read fault, and
the cause is logged" red.
11. Credential read fault answers the AI service account refusal
instead: the credential twin red.
12. The cause log removed (`console.warn(` → `String(`): both read-fault
pins red. A first attempt at this leg (`void (` with a trailing comma)
failed to transform, so it measured nothing; it was replaced by this
one.
</details>
<details><summary>Dispatch gates</summary>
`node scripts/pm/dispatch-gates.mjs --repo objectstack-ai/objectstack
--ran`: 77 derived, 77 run, 0 NOT-MEASURED, every exit 0.
- `check:dispatcher-error-vocabulary` first flagged the three field
codes stamped through a parameter; the refusal helper now takes one
literal ADR-0114 field entry, and the gate passes.
- `check:dual-build-cjs-loads` and the teardown self-test ran after a
full build and a fetch of their pinned commit.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmPK8RUPmLyYjQGCe58
<!-- signed: domain:server#1 · os-steve ·
session_01ADHkmPK8RUPmLyYjQGCe58 -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent 75d7fb1 commit 98ba756
23 files changed
Lines changed: 1232 additions & 8 deletions
File tree
- .changeset
- packages
- platform-objects/src
- apps/translations
- identity
- plugins/plugin-auth/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| 177 | + | |
177 | 178 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
0 commit comments