重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
explanation-docs by existential-birds/beagle
npx skills add https://github.com/existential-birds/beagle --skill explanation-docs此技能提供了编写有效解释性文档的模式。解释是针对那些想要了解事物为何如此运作的读者的理解导向内容。
目标读者:
解释性文档用于在远离键盘时阅读。 与教程或操作指南不同,读者在阅读时并非试图完成某项任务。他们正在构建心智模型。
解释性文档不是:
所有解释性文档请使用此结构:
---
title: "[概念/系统名称] 详解"
description: "了解 [概念] 如何工作以及为何如此设计"
---
# 理解 [概念]
简短介绍(2-3 句):本文档解释什么以及为何重要。设定读者阅读后将理解内容的期望。
## 概述
概念的高级摘要。它是什么?解决了什么问题?即使没有深厚的技术知识也应能理解。
## 背景与上下文
### 问题所在
是什么情况或挑战导致了这种设计?用户或开发者当时在为何而挣扎?
### 历史背景
我们是如何走到这一步的?之前是什么情况?这有助于读者理解为何替代方案被拒绝或为何存在某些约束。
## 工作原理
### 核心概念
解释基本思想。使用类比来连接读者已经理解的概念。
<Note>
在解释复杂关系或流程时,使用图表或视觉辅助工具。
</Note>
### 机制
逐步讲解系统实际如何运作。这是概念性的,而非过程性的——解释“发生了什么”而不是“该做什么”。
### 关键组件
分解主要部分及其交互方式。对于每个组件:
- 它扮演什么角色?
- 它如何与其他组件关联?
## 设计决策与权衡
### 为何采用此方法?
解释关键设计选择背后的理由。哪些目标驱动了这些决策?
### 所做的权衡
每个设计都涉及权衡。明确说明:
- 优先考虑了哪些方面
- 牺牲了哪些方面
- 在什么条件下此设计表现出色或存在不足
### 约束与假设
哪些约束塑造了设计?它依赖于哪些假设?
## 考虑过的替代方案
### [替代方案 1]
替代方案的简要描述。为何未被选中?在什么情况下它可能更好?
### [替代方案 2]
另一个替代方案。比较替代方案有助于读者理解设计空间。
## 影响与后果
这种设计对以下方面意味着什么:
- 性能?
- 可扩展性?
- 开发者体验?
- 未来可扩展性?
## 相关概念
- [相关概念 1](/concepts/related-1) - 它如何与此主题连接
- [相关概念 2](/concepts/related-2) - 另一个相关领域
- [更深入的技术参考](/reference/detail) - 用于实现细节
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
解释回答“为什么?”和“它是如何工作的?”,而不是“我该怎么做?”
| 解释(良好) | 操作指南(错误上下文) |
|---|---|
| "缓存使用 LRU 淘汰策略,因为内存有限且最近访问的项更可能被再次访问。" | "要配置缓存,请设置 maxSize 参数。" |
| "身份验证令牌会过期,以限制其泄露时可能造成的损害。" | "通过调用 /refresh 端点来刷新您的令牌。" |
将不熟悉的概念与读者已知的事物联系起来。
<!-- 良好:相关的类比 -->
将消息队列想象成一个邮局。消息(信件)由发送者投递,并保存直到接收者取走。邮局不关心内容——它只确保可靠的传递。
<!-- 避免:直接跳入技术细节 -->
消息队列实现了一个具有可配置持久性和至少一次传递语义的 FIFO 缓冲区。
不要仅仅描述存在什么——解释它为何以那种方式存在。
<!-- 良好:解释理由 -->
我们选择最终一致性而非强一致性,是因为我们的读密集型工作负载(读写比为 100:1)从低延迟中获益更多,而不是从即时一致性中获益。大多数用户从未注意到短暂的延迟。
<!-- 避免:仅陈述事实 -->
系统使用最终一致性,传播窗口为 500 毫秒。
每个设计选择都有成本。承认它们可以建立信任并帮助读者做出明智的决策。
## 权衡
此架构优化了**写入吞吐量**,代价是:
- **读取延迟**:查询可能需要访问多个分区
- **复杂性**:开发者必须理解分区键
- **成本**:由于反规范化导致更多存储
这种权衡适合我们的用例(高容量事件摄取),但可能不适合读密集型的分析工作负载。
解释性文档是线性阅读的,远离键盘。将它们构建得像文章,而非手册。
展示此概念如何与系统的其他部分或更广泛的行业模式相关联。
## 相关概念
我们的事件溯源方法是更广泛的 CQRS(命令查询职责分离)架构的一部分。理解事件溯源有助于解释:
- 为什么我们的读取模型是最终一致的
- 我们如何“免费”实现审计日志记录
- 为什么重放事件是我们测试策略的核心
有关 CQRS 的更多信息,请参阅[理解我们的架构](/concepts/cqrs-architecture)。
解释性文档极大地受益于视觉辅助:
## 系统架构
下图显示了请求如何在系统中流动:
```mermaid
graph LR
A[Client] --> B[Load Balancer]
B --> C[API Gateway]
C --> D[Service A]
C --> E[Service B]
D --> F[(Database)]
E --> F
负载均衡器将流量分发到 API 网关实例...
### 比较表格
表格非常适合比较方法:
```markdown
## 方法比较
| 方面 | 单体架构 | 微服务 |
|--------|----------|---------------|
| 部署 | 单一单元,更简单 | 独立,更复杂 |
| 扩展 | 垂直扩展 | 按服务水平扩展 |
| 团队自主权 | 较低 | 较高 |
| 运维开销 | 较低 | 较高 |
我们选择了微服务,因为团队自主权对我们拥有 100 多名工程师的组织至关重要...
<Note>
这是一个常见的混淆来源:最终一致性中的“最终”并不意味着“可能”——它意味着“不是立即,但最终保证会一致”。
</Note>
<Warning>
此设计假设网络分区很少发生。在网络不可靠的环境中,请考虑更强的保证一致性。
</Warning>
使用可展开部分来处理相关但有价值的细节:
<Expandable title="历史说明:为何我们从 Redis 迁移">
我们最初的实现使用 Redis 进行缓存。在 2023 年,我们迁移到了一个自定义解决方案,因为...
这个背景解释了为什么一些旧代码仍然引用 Redis 模式,即使我们不再直接使用它。
</Expandable>
---
title: "理解我们的身份验证系统"
description: "了解我们平台中的身份验证如何工作以及我们为何如此设计"
---
# 理解我们的身份验证系统
本文档解释了我们的身份验证系统如何工作以及其设计背后的原因。阅读后,您将理解从登录到 API 访问的流程,以及我们为何做出这些架构选择。
## 概述
我们的身份验证系统使用短寿命的访问令牌和长寿命的刷新令牌。这种模式有时被称为“令牌轮换”,通过限制暴露同时避免频繁重新身份验证,在安全性和用户体验之间取得平衡。
## 背景与上下文
### 问题所在
现代 Web 应用程序面临相互竞争的需求:安全团队希望频繁轮换凭据,而用户期望无需频繁登录的无缝体验。传统的基于会话的身份验证需要服务器端状态,使水平扩展复杂化。
### 历史背景
我们最初使用存储在 Redis 中的服务器端会话。随着我们扩展到多个区域,会话同步成为瓶颈。JWT 令牌作为无状态身份验证的行业标准出现,我们在 2022 年采用了它们。
## 工作原理
### 核心概念
**访问令牌**就像会议的一日通行证。它们在有限时间内授予入场权限,并在每个门口(API 端点)检查。如果有人偷了您的一日通行证,他们只能在它过期前使用它。
**刷新令牌**就像您用来获取一日通行证的注册确认函。您不会随身携带它,但当您的通行证过期时,您可以用它来获取一个新的。
### 身份验证流程
当用户登录时:
1. 他们向身份验证服务提供凭据
2. 如果有效,他们会收到一个访问令牌(15 分钟过期)和一个刷新令牌(7 天过期)
3. 访问令牌用于 API 请求
4. 当访问令牌过期时,刷新令牌用于获取一个新的
5. 旧的刷新令牌失效,并颁发一个新的
这种轮换意味着即使刷新令牌被泄露,在合法用户的下一次刷新使其失效之前,它只能被使用一次。
### 关键组件
**身份验证服务**:颁发和验证令牌。对于访问令牌是无状态的,维护已撤销刷新令牌的拒绝列表。
**API 网关**:验证每个请求的访问令牌。在请求到达后端服务之前拒绝过期或格式错误的令牌。
**令牌存储**:维护刷新令牌的元数据以进行撤销。使用具有区域复制的 Redis。
## 设计决策与权衡
### 为何使用短寿命的访问令牌?
我们根据威胁模型选择了 15 分钟的过期时间。更短的过期时间限制了被盗令牌滥用的窗口,但更频繁的刷新会增加延迟和身份验证服务负载。我们的分析显示,15 分钟对于我们的流量模式平衡了这些顾虑。
### 所做的权衡
**优先考虑**:水平可扩展性,通过令牌轮换实现安全性
**牺牲**:访问令牌的立即撤销,简单性
即使注销后,访问令牌在过期前仍然有效。对于大多数用例,15 分钟的持续访问是可接受的。对于高安全性操作(密码更改、大额转账),我们要求重新身份验证。
### 约束与假设
- 客户端可以安全地存储刷新令牌(Web 使用 HttpOnly cookie)
- 服务器之间的时钟偏差在 30 秒以内
- Redis 可用于刷新令牌验证
## 考虑过的替代方案
### 服务器端会话
传统会话将允许立即撤销,但需要粘性会话或分布式会话存储。由于扩展复杂性和区域延迟问题,我们拒绝了此方案。
### 更长的访问令牌过期时间
寿命更长的令牌减少了身份验证服务负载,但增加了令牌被盗的风险。考虑到我们的安全要求,我们优先选择了更短的窗口。
## 影响与后果
**性能**:身份验证服务每分钟处理约 10K 个刷新请求。令牌验证是 CPU 密集型的(签名验证),因此我们进行水平扩展。
**开发者体验**:服务永远不需要为身份验证而访问数据库——它们只需验证 JWT 签名。这简化了服务开发。
**用户体验**:大多数用户从未注意到令牌刷新。移动应用程序会主动刷新以避免在操作中途过期。
## 相关概念
- [API 网关架构](/concepts/api-gateway) - 网关如何验证令牌
- [令牌安全最佳实践](/concepts/token-security) - 安全存储指南
- [身份验证 API 参考](/reference/auth-api) - 端点文档
发布前,请验证:
| 读者的问题 | 文档类型 | 重点 |
|---|---|---|
| "我如何做 X?" | 操作指南 | 实现目标的步骤 |
| "教我关于 X" | 教程 | 通过引导实践学习 |
| "X 的 API 是什么?" | 参考文档 | 精确的技术细节 |
| "X 为什么这样工作?" | 解释性文档 | 理解和上下文 |
| "X 的权衡是什么?" | 解释性文档 | 设计理由 |
| "X 与 Y 有何关系?" | 解释性文档 | 概念连接 |
当用户出现以下情况时,编写解释性文档:
如果用户在阅读时需要完成某事,则它不是解释性文档:
每周安装数
62
代码仓库
GitHub 星标数
45
首次出现
2026年1月24日
安全审计
安装于
gemini-cli49
codex49
opencode48
cursor44
github-copilot44
claude-code44
This skill provides patterns for writing effective explanation documents. Explanations are understanding-oriented content for readers who want to know why things work the way they do.
Target readers:
Explanations are for reading away from the keyboard. Unlike tutorials or how-to guides, readers aren't trying to accomplish a task while reading. They're building mental models.
Explanations are NOT:
Use this structure for all explanation documents:
---
title: "[Concept/System Name] Explained"
description: "Understand how [concept] works and why it was designed this way"
---
# Understanding [Concept]
Brief intro (2-3 sentences): What this document explains and why it matters. Set expectations for what the reader will understand after reading.
## Overview
High-level summary of the concept. What is it? What problem does it solve? This should be understandable without deep technical knowledge.
## Background and Context
### The Problem
What situation or challenge led to this design? What were users or developers struggling with?
### Historical Context
How did we get here? What came before? This helps readers understand why alternatives were rejected or why certain constraints exist.
## How It Works
### Core Concepts
Explain the fundamental ideas. Use analogies to connect to concepts readers already understand.
<Note>
Use diagrams or visual aids when explaining complex relationships or flows.
</Note>
### The Mechanism
Walk through how the system actually operates. This is conceptual, not procedural - explain the "what happens" rather than "what to do."
### Key Components
Break down the major parts and how they interact. For each component:
- What role does it play?
- How does it relate to other components?
## Design Decisions and Trade-offs
### Why This Approach?
Explain the reasoning behind key design choices. What goals drove these decisions?
### Trade-offs Made
Every design involves trade-offs. Be explicit about:
- What was prioritized
- What was sacrificed
- Under what conditions this design excels or struggles
### Constraints and Assumptions
What constraints shaped the design? What assumptions does it rely on?
## Alternatives Considered
### [Alternative Approach 1]
Brief description of an alternative approach. Why wasn't it chosen? Under what circumstances might it be better?
### [Alternative Approach 2]
Another alternative. Comparing alternatives helps readers understand the design space.
## Implications and Consequences
What does this design mean for:
- Performance?
- Scalability?
- Developer experience?
- Future extensibility?
## Related Concepts
- [Related Concept 1](/concepts/related-1) - How it connects to this topic
- [Related Concept 2](/concepts/related-2) - Another related area
- [Deeper Technical Reference](/reference/detail) - For implementation specifics
Explanations answer "why?" and "how does it work?" rather than "how do I?"
| Explanation (good) | How-To (wrong context) |
|---|---|
| "The cache uses LRU eviction because memory is limited and recent items are more likely to be accessed again." | "To configure the cache, set the maxSize parameter." |
| "Authentication tokens expire to limit the damage if they're compromised." | "Refresh your token by calling the /refresh endpoint." |
Connect unfamiliar concepts to things readers already know.
<!-- Good: Relatable analogy -->
Think of the message queue like a post office. Messages (letters) are dropped off
by senders and held until recipients pick them up. The post office doesn't care
about the content - it just ensures reliable delivery.
<!-- Avoid: Jumping straight to technical details -->
The message queue implements a FIFO buffer with configurable persistence
and at-least-once delivery semantics.
Don't just describe what exists - explain why it exists that way.
<!-- Good: Explains rationale -->
We chose eventual consistency over strong consistency because our read-heavy
workload (100:1 read-to-write ratio) benefits more from low latency than from
immediate consistency. Most users never notice the brief delay.
<!-- Avoid: Just states facts -->
The system uses eventual consistency with a 500ms propagation window.
Every design choice has costs. Acknowledging them builds trust and helps readers make informed decisions.
## Trade-offs
This architecture optimizes for **write throughput** at the cost of:
- **Read latency**: Queries may need to hit multiple partitions
- **Complexity**: Developers must understand partition keys
- **Cost**: More storage due to denormalization
This trade-off makes sense for our use case (high-volume event ingestion)
but may not suit read-heavy analytics workloads.
Explanations are read linearly, away from the keyboard. Structure them like essays, not manuals.
Show how this concept relates to other parts of the system or to broader industry patterns.
## Related Concepts
Our event sourcing approach is part of our broader CQRS (Command Query
Responsibility Segregation) architecture. Understanding event sourcing
helps explain:
- Why our read models are eventually consistent
- How we achieve audit logging "for free"
- Why replaying events is central to our testing strategy
For more on CQRS, see [Understanding Our Architecture](/concepts/cqrs-architecture).
Explanations benefit heavily from visual aids:
## System Architecture
The following diagram shows how requests flow through the system:
```mermaid
graph LR
A[Client] --> B[Load Balancer]
B --> C[API Gateway]
C --> D[Service A]
C --> E[Service B]
D --> F[(Database)]
E --> F
The load balancer distributes traffic across API gateway instances...
### Comparison Tables
Tables work well for comparing approaches:
```markdown
## Comparing Approaches
| Aspect | Monolith | Microservices |
|--------|----------|---------------|
| Deployment | Single unit, simpler | Independent, more complex |
| Scaling | Vertical | Horizontal per service |
| Team autonomy | Lower | Higher |
| Operational overhead | Lower | Higher |
We chose microservices because team autonomy was critical for our
100+ engineer organization...
<Note>
This is a common source of confusion: the "eventual" in eventual consistency
doesn't mean "maybe" - it means "not immediately, but guaranteed eventually."
</Note>
<Warning>
This design assumes network partitions are rare. In environments with
unreliable networks, consider stronger consistency guarantees.
</Warning>
Use expandables for tangential but valuable details:
<Expandable title="Historical note: Why we migrated from Redis">
Our original implementation used Redis for caching. In 2023, we migrated
to a custom solution because...
This context explains why some older code references Redis patterns
even though we no longer use it directly.
</Expandable>
---
title: "Understanding Our Authentication System"
description: "Learn how authentication works in our platform and why we designed it this way"
---
# Understanding Our Authentication System
This document explains how our authentication system works and the reasoning
behind its design. After reading, you'll understand the flow from login to
API access and why we made the architectural choices we did.
## Overview
Our authentication system uses short-lived access tokens with long-lived refresh
tokens. This pattern, sometimes called "token rotation," balances security with
user experience by limiting exposure while avoiding frequent re-authentication.
## Background and Context
### The Problem
Modern web applications face competing demands: security teams want frequent
credential rotation, while users expect seamless experiences without constant
logins. Traditional session-based authentication requires server-side state,
complicating horizontal scaling.
### Historical Context
We originally used server-side sessions stored in Redis. As we scaled to
multiple regions, session synchronization became a bottleneck. JWT tokens
emerged as an industry standard for stateless authentication, and we adopted
them in 2022.
## How It Works
### Core Concepts
**Access tokens** are like day passes at a conference. They grant entry for a
limited time and are checked at each door (API endpoint). If someone steals
your day pass, they can only use it until it expires.
**Refresh tokens** are like the registration confirmation you used to get your
day pass. You don't carry it around, but you can use it to get a new day pass
when yours expires.
### The Authentication Flow
When a user logs in:
1. They provide credentials to the authentication service
2. If valid, they receive both an access token (15-minute expiry) and
a refresh token (7-day expiry)
3. The access token is used for API requests
4. When the access token expires, the refresh token obtains a new one
5. The old refresh token is invalidated, and a new one is issued
This rotation means that even if a refresh token is compromised, it can only
be used once before the legitimate user's next refresh invalidates it.
### Key Components
**Authentication Service**: Issues and validates tokens. Stateless for access
tokens, maintains a denylist for revoked refresh tokens.
**API Gateway**: Validates access tokens on every request. Rejects expired or
malformed tokens before requests reach backend services.
**Token Store**: Maintains refresh token metadata for revocation. Uses Redis
with regional replication.
## Design Decisions and Trade-offs
### Why Short-Lived Access Tokens?
We chose 15-minute expiry based on our threat model. Shorter expiry limits the
window for stolen token abuse, but more frequent refreshes increase latency
and auth service load. Our analysis showed 15 minutes balances these concerns
for our traffic patterns.
### Trade-offs Made
**Prioritized**: Horizontal scalability, security through token rotation
**Sacrificed**: Immediate revocation of access tokens, simplicity
Access tokens remain valid until expiry even after logout. For most use cases,
15 minutes of continued access is acceptable. For high-security operations
(password changes, large transfers), we require re-authentication.
### Constraints and Assumptions
- Clients can securely store refresh tokens (HttpOnly cookies for web)
- Clock skew between servers is under 30 seconds
- Redis is available for refresh token validation
## Alternatives Considered
### Server-Side Sessions
Traditional sessions would allow immediate revocation but require sticky
sessions or distributed session storage. We rejected this due to scaling
complexity and regional latency concerns.
### Longer Access Token Expiry
Longer-lived tokens reduce auth service load but increase risk from token
theft. Given our security requirements, we prioritized shorter windows.
## Implications and Consequences
**Performance**: Auth service handles ~10K refresh requests per minute. Token
validation is CPU-bound (signature verification), so we scale horizontally.
**Developer Experience**: Services never need database access for auth - they
just validate JWT signatures. This simplifies service development.
**User Experience**: Most users never notice token refresh. Mobile apps
refresh proactively to avoid mid-action expiry.
## Related Concepts
- [API Gateway Architecture](/concepts/api-gateway) - How the gateway validates tokens
- [Token Security Best Practices](/concepts/token-security) - Secure storage guidance
- [Authentication API Reference](/reference/auth-api) - Endpoint documentation
Before publishing, verify:
| Reader's Question | Doc Type | Focus |
|---|---|---|
| "How do I do X?" | How-To Guide | Steps to accomplish a goal |
| "Teach me about X" | Tutorial | Learning through guided doing |
| "What is the API for X?" | Reference | Precise technical details |
| "Why does X work this way?" | Explanation | Understanding and context |
| "What are the trade-offs of X?" | Explanation | Design rationale |
| "How does X relate to Y?" | Explanation | Conceptual connections |
Write an explanation when users:
If users need to accomplish something while reading, it's not an explanation:
Weekly Installs
62
Repository
GitHub Stars
45
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli49
codex49
opencode48
cursor44
github-copilot44
claude-code44
API文档生成与管理指南:OpenAPI规范、Swagger使用教程与团队协作
11,700 周安装
Dodo Payments 订阅集成指南:实现试用期、计划变更和基于使用量的循环计费
274 周安装
Claude AI 内存维护指南 - 优化向量存储,防止令牌浪费,提升性能
270 周安装
iOS 玻璃质感 UI 设计师 | 遵循 Apple 设计规范的原生界面设计助手
274 周安装
Java JPA Hibernate 实战:掌握数据持久化、查询优化与缓存策略
273 周安装
PixiJS 2D 渲染引擎教程:高性能 WebGL/WebGPU 图形、粒子效果与交互式应用开发
283 周安装
Android开发最佳实践:遵循Google官方架构指南,构建模块化现代应用
274 周安装