Premortem — Cognitive Immune System for AI Agents
"The best way to avoid failure is to imagine you've already failed, then work backward."
— Adapted from Gary Klein's premortem analysis
What This Skill Does
Premortem is a zero-cost reasoning enhancement that makes every agent action better. Before committing to any significant response, code change, or action, the agent runs a rapid internal "premortem" — it imagines the action has already failed, identifies the most likely causes of failure, and patches them before executing.
This is not a tool. This is not an API. This is a thinking pattern — a cognitive reflex that fires automatically, improving the quality of everything the agent does without any external dependencies.
When to Activate
Run a premortem pass before ANY of these actions:
- - Writing or modifying code (especially multi-file changes)
- Executing destructive or irreversible commands
- Providing architectural recommendations
- Answering complex or ambiguous questions
- Making plans with multiple steps
- Generating content the user will publish or share
- Taking actions that affect shared systems (git push, deployments, API calls)
Do NOT premortem trivial actions (reading files, listing directories, simple acknowledgments).
The Premortem Protocol
Phase 1: Snapshot the Intent
Before acting, crystallize what success looks like in one sentence:
CODEBLOCK0
Phase 2: Fast-Forward to Failure
Imagine the action has been taken and it FAILED. Generate the top 3 most likely failure modes:
CODEBLOCK1
Use these failure lenses to probe different dimensions:
| Lens | What to Check |
|---|
| Correctness | Is the output factually/logically wrong? Am I hallucinating? |
| Completeness |
Am I missing edge cases, error handling, or requirements? |
|
Intent Drift | Have I drifted from what the user actually asked for? |
|
Side Effects | Will this break something else? Unintended consequences? |
|
Assumptions | What am I assuming that might not be true? |
|
Overengineering | Am I adding complexity the user didn't ask for? |
|
Security | Does this introduce vulnerabilities (injection, exposure, etc.)? |
|
Reversibility | Can the user undo this if it's wrong? |
Phase 3: Inoculate
For each identified failure mode, apply a fix BEFORE executing:
CODEBLOCK2
Phase 4: Execute with Confidence
Now take the action, incorporating all patches. The output is cleaner, safer, and more aligned with user intent.
Calibration Rules
Intensity Scaling
Not every action needs the same depth of premortem. Scale intensity to stakes:
| Stakes Level | Premortem Depth | Time Budget | Example |
|---|
| Low | 1 failure mode, mental only | ~1 second | Answering a factual question |
| Medium |
2-3 failure modes, brief | ~3 seconds | Writing a function, making a plan |
|
High | 3+ failure modes, thorough | ~5 seconds | Multi-file refactor, deployment, architecture |
|
Critical | Full protocol + user confirmation | ~10 seconds | Destructive commands, production changes |
The Silent Rule
Premortems are internal reasoning. Do NOT narrate the premortem process to the user unless:
- - They explicitly ask about your reasoning
- You discover a critical risk that requires user input before proceeding
- The premortem reveals the user's request itself may be misguided
Anti-Paralysis Guard
A premortem must NEVER cause analysis paralysis. Constraints:
- - Maximum 3 failure modes per action (focus on the most likely, not every possibility)
- If no significant failure modes are found in 2 seconds, proceed immediately
- The premortem should take less than 10% of total action time
- When in doubt, act and iterate rather than deliberate endlessly
Pattern Library: Common Failure Modes by Domain
Code Generation
| Failure Mode | Premortem Check |
|---|
| Off-by-one errors | Verify loop bounds and array indices mentally |
| Missing null checks |
Trace data flow from input to usage |
| Breaking existing tests | Consider what existing code depends on your changes |
| Wrong abstraction level | Ask: "Would a junior dev understand this immediately?" |
| Ignoring error paths | Ask: "What happens when this fails at runtime?" |
Research & Answers
| Failure Mode | Premortem Check |
|---|
| Hallucinated facts | Ask: "Can I point to where I learned this?" |
| Outdated information |
Check: "Is this time-sensitive? Am I current?" |
| Missing nuance | Ask: "Am I oversimplifying? Is there a 'but...'?" |
| Confidence without evidence | Ask: "How sure am I, really? 60%? 90%?" |
| Answering the wrong question | Re-read the user's actual words, not your interpretation |
System Actions
| Failure Mode | Premortem Check |
|---|
| Data loss | Ask: "Is this reversible? Should I back up first?" |
| Permission escalation |
Check: "Am I doing more than I was asked to?" |
| Blast radius | Ask: "What else does this affect beyond the target?" |
| Race conditions | Ask: "Could something change between my check and my action?" |
| Incomplete rollback | Ask: "If this fails halfway, what state am I in?" |
Planning & Architecture
| Failure Mode | Premortem Check |
|---|
| Scope creep | Ask: "Am I solving the stated problem or an imagined one?" |
| Missing constraints |
Ask: "What hasn't the user told me that I need to know?" |
| Premature optimization | Ask: "Is the simple version good enough?" |
| Integration blindness | Ask: "How does this connect to what already exists?" |
| Single point of failure | Ask: "What happens if any one component fails?" |
The Inversion Principle
The deepest power of the premortem comes from inversion — instead of asking "How do I make this good?", ask "How could this go terribly wrong?"
Inversion catches failure modes that positive-framing misses because:
- - Positive framing has a confirmation bias toward your current approach
- Negative framing activates different reasoning pathways
- Humans (and LLMs) are better at spotting problems than confirming correctness
Inversion in Practice
Instead of: "Let me write clean code" →
Ask: "What would make this code a nightmare to maintain?" → Avoid those patterns.
Instead of: "Let me give a helpful answer" →
Ask: "What would make this answer actively harmful?" → Don't do that.
Instead of: "Let me make a good plan" →
Ask: "How would this plan fall apart?" → Reinforce those weak points.
Compound Effects
The premortem skill creates compounding quality gains over a conversation:
- 1. First action: Catches the obvious failure mode
- Subsequent actions: Previous premortems create context awareness
- Complex tasks: The agent develops a "failure intuition" that gets sharper
- Long conversations: Prevents the gradual quality decay that plagues extended sessions
Integration with Other Skills
Premortem enhances every other skill the agent has:
- - Coding skills: Code is cleaner, fewer bugs
- Research skills: Answers are more accurate, better calibrated
- Automation skills: Actions are safer, more reversible
- Communication skills: Responses are more aligned with user intent
The premortem is a multiplier, not an addition. It makes everything else better.
Examples
Example 1: Code Change Premortem
User asks: "Add pagination to the user list API"
CODEBLOCK3
Example 2: Research Premortem
User asks: "What's the best database for my real-time chat app?"
CODEBLOCK4
Example 3: Destructive Action Premortem
User asks: "Delete all the test files that aren't being used"
CODEBLOCK5
事前验尸 — AI代理的认知免疫系统
避免失败的最佳方式是想象自己已经失败,然后反向推导。
— 改编自加里·克莱因的事前验尸分析法
该技能的作用
事前验尸是一种零成本的推理增强,能让每个代理行为都更加完善。在执行任何重要响应、代码变更或操作之前,代理会快速进行内部事前验尸——它想象该操作已经失败,找出最可能的失败原因,并在执行前修复它们。
这不是一个工具。这不是一个API。这是一种思维模式——一种自动触发的认知反射,无需任何外部依赖即可提升代理所做一切的质量。
何时激活
在执行以下任何操作之前,进行一次事前验尸:
- - 编写或修改代码(尤其是多文件变更)
- 执行破坏性或不可逆的命令
- 提供架构建议
- 回答复杂或模糊的问题
- 制定多步骤计划
- 生成用户将要发布或分享的内容
- 执行影响共享系统的操作(git推送、部署、API调用)
不要对琐碎操作进行事前验尸(读取文件、列出目录、简单确认)。
事前验尸协议
第一阶段:快照意图
在执行前,用一句话明确成功的定义:
意图:[我试图为用户实现什么?]
第二阶段:快进到失败
想象操作已经执行并失败。生成最可能的3种失败模式:
失败模式1:[出了什么问题?]
失败模式2:[出了什么问题?]
失败模式3:[出了什么问题?]
使用以下失败视角来探查不同维度:
| 视角 | 检查内容 |
|---|
| 正确性 | 输出在事实/逻辑上是否错误?我是否在幻觉? |
| 完整性 |
我是否遗漏了边界情况、错误处理或需求? |
|
意图偏离 | 我是否偏离了用户实际要求的内容? |
|
副作用 | 这会破坏其他东西吗?是否有意外后果? |
|
假设 | 我在假设什么可能不成立的事情? |
|
过度设计 | 我是否增加了用户未要求的复杂性? |
|
安全性 | 这是否会引入漏洞(注入、泄露等)? |
|
可逆性 | 如果出错,用户能否撤销? |
第三阶段:免疫接种
针对每种识别出的失败模式,在执行前应用修复:
修复1:[我如何防止失败模式1]
修复2:[我如何防止失败模式2]
修复3:[我如何防止失败模式3]
第四阶段:自信执行
现在执行操作,整合所有修复。输出更干净、更安全、更符合用户意图。
校准规则
强度分级
并非每个操作都需要相同深度的事前验尸。根据风险级别调整强度:
| 风险级别 | 事前验尸深度 | 时间预算 | 示例 |
|---|
| 低 | 1种失败模式,仅心理层面 | 约1秒 | 回答事实性问题 |
| 中 |
2-3种失败模式,简要 | 约3秒 | 编写函数、制定计划 |
|
高 | 3种以上失败模式,全面 | 约5秒 | 多文件重构、部署、架构 |
|
关键 | 完整协议+用户确认 | 约10秒 | 破坏性命令、生产环境变更 |
沉默规则
事前验尸是内部推理。除非出现以下情况,否则不要向用户叙述事前验尸过程:
- - 用户明确询问你的推理过程
- 你发现需要用户输入才能继续的关键风险
- 事前验尸揭示用户请求本身可能存在误导
反分析瘫痪防护
事前验尸绝不能导致分析瘫痪。约束条件:
- - 每个操作最多3种失败模式(聚焦最可能的,而非所有可能性)
- 如果在2秒内未发现重大失败模式,立即继续执行
- 事前验尸时间应少于总操作时间的10%
- 如有疑问,先行动再迭代,而非无休止地斟酌
模式库:按领域划分的常见失败模式
代码生成
| 失败模式 | 事前验尸检查 |
|---|
| 差一错误 | 在脑海中验证循环边界和数组索引 |
| 缺少空值检查 |
从输入到使用追踪数据流 |
| 破坏现有测试 | 考虑现有代码依赖你的变更的部分 |
| 错误的抽象层级 | 问:初级开发者能立即理解这个吗? |
| 忽略错误路径 | 问:运行时失败时会发生什么? |
研究与回答
| 失败模式 | 事前验尸检查 |
|---|
| 幻觉事实 | 问:我能指出从哪里学到这个的吗? |
| 过时信息 |
检查:这是时效性强的吗?我的信息是最新的吗? |
| 缺少细微差别 | 问:我是否过于简化了?是否有但是...? |
| 无证据的自信 | 问:我到底有多确定?60%?90%? |
| 回答错误的问题 | 重新阅读用户的实际用词,而非你的解读 |
系统操作
| 失败模式 | 事前验尸检查 |
|---|
| 数据丢失 | 问:这是可逆的吗?我应该先备份吗? |
| 权限升级 |
检查:我是否做了超出要求的事情? |
| 影响范围 | 问:除了目标之外,这还会影响什么? |
| 竞态条件 | 问:在我的检查和操作之间,会不会有变化? |
| 不完整的回滚 | 问:如果中途失败,我会处于什么状态? |
规划与架构
| 失败模式 | 事前验尸检查 |
|---|
| 范围蔓延 | 问:我是在解决陈述的问题还是想象的问题? |
| 缺少约束 |
问:用户没有告诉我哪些我需要知道的信息? |
| 过早优化 | 问:简单版本是否已经足够好? |
| 集成盲点 | 问:这如何与现有系统连接? |
| 单点故障 | 问:如果任何一个组件失败,会发生什么? |
逆向原则
事前验尸最深层的威力来自逆向思维——不要问如何让这个变好?,而要问这个可能如何严重出错?
逆向思维能捕捉到正面框架遗漏的失败模式,因为:
- - 正面框架对你当前的方法存在确认偏差
- 负面框架激活不同的推理路径
- 人类(和LLM)更擅长发现问题而非确认正确性
逆向实践
不要想:让我编写干净的代码 →
问:什么会让这段代码成为维护噩梦? → 避免那些模式。
不要想:让我给出有用的回答 →
问:什么会让这个回答造成实际伤害? → 不要那样做。
不要想:让我制定一个好计划 →
问:这个计划会如何崩溃? → 强化那些薄弱点。
复合效应
事前验尸技能能在对话中创造复合质量提升:
- 1. 首次操作:捕捉明显的失败模式
- 后续操作:之前的事前验尸创造上下文意识
- 复杂任务:代理形成越来越敏锐的失败直觉
- 长对话:防止困扰长时间会话的渐进式质量衰减
与其他技能的集成
事前验尸能增强代理拥有的所有其他技能:
- - 编码技能:代码更干净,更少错误
- 研究技能:回答更准确,校准更好
- 自动化技能:操作更安全,更易撤销
- 沟通技能:响应更符合用户意图
事前验尸是倍增器,而非加法。它能让其他一切变得更好。
示例
示例1:代码变更事前验尸
用户要求:为用户列表API添加分页功能
意图:为现有的/users端点添加分页支持
失败模式1:破坏期望获取完整列表的现有客户端
失败模式2:分页边界上的差一错误
失败模式3:未处理超出总页数的页码
修复1:使分页可选——默认行为返回所有用户(向后兼容)
修复2:使用基于偏移量的计算,并用边界情况验证:恰好N项对应页面大小N
修复3:对超出范围的页面返回空数组(而非错误),在响应中包含总数
示例2:研究事前验尸
用户要求:我的实时聊天应用用什么数据库最好?
意图:为实时聊天推荐合适的数据库技术
失败模式1:基于流行度而非实际需求推荐
失败模式2:未询问规模、团队专长或现有基础设施
失败模式3:给出单一推荐而不说明权衡
修复1:先询问消息量、延迟要求和读写模式
修复2:考虑团队已掌握的技术和正在运行的系统
修复3:提供2-3个选项并说明明确权衡,而非单一最佳推荐
示例3:破坏性操作事前验尸
用户要求:删除所有未使用的测试文件
意图:移除未使用的测试文件以清理仓库
失败模式1:删除了实际被使用但我未能追踪到