@happyvertical/smrt-approvals

中文文档
0.62.1发布于 2026/11/17MITTypeScriptES Module近 30 天下载 1.4万

Approval decision gate for the SMRT framework: revision-bound, single-use approvals with quorum, expiry, and an append-only event ledger

原始使用文档
文档目录8 节
  1. Install
  2. Concepts
  3. Usage
  4. Guarantees
  5. Permissions
  6. Development
  7. License
  8. Optional self-approval

@happyvertical/smrt-approvals

A small decision gate for s-m-r-t applications. A package defines an approval kind; a requester opens a request bound to one revision of one subject; eligible people decide; the consumer consumes the approval exactly once and then performs its own action. It is not a workflow engine: there are no stages, no executor, and no compensation.

Install

pnpm add @happyvertical/smrt-approvals

Concepts

ModelPurpose
ApprovalRequestOne gate on one subject revision: kind, subject type and id, revision hash, requester, status, quorum, expiry, decision and consumption timestamps, tenant.
ApprovalEventAppend-only ledger: created, approved, rejected, changes_requested, cancelled, expired, consumed, with actor and reason. The source of truth.
ApprovalPolicyA tenant's tightening of a kind: a higher quorum, a shorter expiry, or one more required permission. It can never loosen the package defaults.

Status moves from pending to approved, rejected, changes_requested, cancelled, or expired. An approved request is consumed once (it keeps the approved status and gains consumedAt) or expires unconsumed. A request that needs changes is finished; the requester opens a new request for the new revision.

Usage

import {
  ApprovalService,
  defineApprovalKind,
} from '@happyvertical/smrt-approvals';

// In the owning package. Register `social.approve-post` with
// registerPermissionDefinitions from @happyvertical/smrt-users.
export const publishPost = defineApprovalKind({
  key: 'social.post.publish',
  subject: '@happyvertical/smrt-social:SocialPost',
  permission: 'social.approve-post',
  defaults: { requiredApprovals: 1, ttlMs: 7 * 24 * 60 * 60 * 1000 },
});

const approvals = new ApprovalService({ db });

// Any principal type may request: human, agent, or service.
const opened = await approvals.requestApproval(author, {
  kind: publishPost,
  subjectId: post.id,
  subjectRevisionHash: revisionHash(post),
  requestKey: `publish:${post.id}:${revisionHash(post)}`,
});

// Only a human of the same tenant holding the
// kind's permission, may decide, once.
await approvals.decide(editor, opened.request.id, { decision: 'approve' });

// The consumer presents the revision it is about to act on.
const used = await approvals.consume(publisher, opened.request.id, revisionHash(post));
if (used.outcome === 'transitioned') {
  await publish(post);
}

A principal is { id, tenantId, type: 'human' | 'agent' | 'service', can(slug) }, built from PermissionResolver.resolvePermissions(userId, tenantId) in production or approvalPrincipalFromPermissions() in tests.

Every transition returns { outcome, request, refusal? }. outcome is transitioned, already_applied (an idempotent requestKey replay), or refused with a typed reason such as not_pending, expired, self_approval, not_human, forbidden, already_decided, revision_mismatch, already_consumed, unbacked_approval, or not_found. A refusal writes nothing.

To consume inside your own transaction, so the domain write and the consumption commit together, pass { transaction: tx } to consume. On SQLite and DuckDB open that transaction with withEmbeddedWriteTransaction from @happyvertical/smrt-core (or SmrtObject.withTransaction()), not a raw db.transaction(), so it holds the embedded write queue.

Expiry: expire(principal, id) and expireDue(principal) record expiry for requests whose deadline passed; a job can call them. Decisions and consumption after the deadline are refused even before the sweep runs.

Guarantees

  • Every transition is one guarded conditional UPDATE (id, tenant, expected status, version, expiry) plus one event insert in the same transaction. Concurrent double approval or double consumption has exactly one winner; the loser is refused cleanly and writes nothing.
  • Quorum counts distinct approvers from the ledger; one actor decides once.
  • Tenant isolation on every read and write: a request id from another tenant is not_found.
  • The generated REST and MCP surface is list and get only, and there is no generated CLI. Events cannot be updated or deleted.
  • Only ApprovalService can create requests and events: a row built directly (new ApprovalRequest(...), ApprovalRequestCollection.create(...), a hand-written ApprovalEvent) is refused at the model layer. consume also refuses an approval its event ledger does not back (unbacked_approval).

Permissions

SlugGrants
per-kind slugDeciding requests of that kind.
approvals.cancel-anyCancelling someone else's pending request.
approvals.manage-policyWriting tenant policy (humans only).

Development

pnpm --filter @happyvertical/smrt-approvals test
pnpm --filter @happyvertical/smrt-approvals typecheck
pnpm --filter @happyvertical/smrt-approvals test:postgres

See AGENTS.md and agents/approvals.md for the design invariants.

License

MIT

Optional self-approval

Set selfApprovalPermission: 'content.approve-self' in defineApprovalKind to allow a human requester holding that slug and all normal decision permissions to approve, reject, or request changes on their own request. Omit it to retain the default self_approval refusal. A self-decision records selfApproved: true and selfApprovalPermission on its immutable event. Its approval counts as one vote and can be consumed normally; larger quorums still require other humans. Deploy manifest migrations for the two new event columns before running the new service. Existing unmarked self events do not count. Recorded votes retain their authority if permissions subsequently change, just like ordinary approvals.

版本与发布记录

@happyvertical/smrt-approvals 共发布 103 个版本,首次发布于 2026/10/15;过去 12 个月发布了 103 个版本。

版本发布日期距今
0.62.1latest2026/11/17今天
0.62.02026/11/17今天
0.61.62026/11/161 天前
0.61.52026/11/161 天前
0.61.42026/11/161 天前
0.61.32026/11/152 天前

关于 @happyvertical/smrt-approvals 的常见问题

@happyvertical/smrt-approvals 是什么?

Approval decision gate for the SMRT framework: revision-bound, single-use approvals with quorum, expiry, and an append-only event ledger

如何安装 @happyvertical/smrt-approvals?

在项目目录运行 npm install @happyvertical/smrt-approvals,或使用 pnpm add / yarn add / bun add。

@happyvertical/smrt-approvals 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@happyvertical/smrt-approvals 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@happyvertical/smrt-approvals 可以商用吗?

@happyvertical/smrt-approvals 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@happyvertical/smrt-approvals 的下载量有多少?

根据 npm 官方下载统计,@happyvertical/smrt-approvals 在 2026/10/17 – 2026/11/15 这 30 天内被下载 13,920 次,日均约 464 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@happyvertical/smrt-approvals 还在维护吗?

最新版本 0.62.1 发布于 2026/11/17(今天);至今共发布 103 个版本,其中过去 12 个月发布了 103 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。