session-guard

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Session Guard

会话防护(Session Guard)

Overview

概述

Long sessions corrupt silently. Context compaction drops instructions unannounced. Hooks don't fix it (confirmed by multiple developers on GitHub issues #19471, #9796, #64171). This skill prevents both through behavioral self-enforcement: monitor health, anchor critical rules through compaction, split before damage occurs. No packages, no databases - pure behavioral enforcement that works in every harness.
长会话会悄然损坏。上下文压缩会在无通知的情况下丢弃指令。钩子(Hooks)无法解决这个问题(已由GitHub问题#19471、#9796、#64171的多位开发者证实)。本技能通过行为自我约束来同时预防这两类问题:监控健康状态,通过压缩锚定关键规则,在损坏发生前拆分会话。无需安装包、无需数据库——纯粹的行为约束机制,可在任何环境中生效。

When to Use

使用场景

  • Session exceeds 40 tool calls
  • Agent contradicts earlier decisions
  • Style/naming conventions start drifting
  • After any context compaction event
  • Task scope growing unbounded
  • 会话工具调用次数超过40次
  • Agent与之前的决策相矛盾
  • 风格/命名约定开始出现偏差
  • 发生任何上下文压缩事件后
  • 任务范围无限制扩大

Health Signals

健康信号

SignalThresholdAction
Tool call count>40YELLOW: checkpoint + recite critical rules
Tool call count>60RED: split or compact with anchor
Agent contradicts earlier decisionAnyVERIFY: re-read source of truth
Style/naming driftAnyRECITE: state the active rules aloud
File read returns unexpected contentAnyRE-READ: don't trust cached state
Task scope growing unboundedContinuousSPLIT: one task per session
信号阈值操作
工具调用次数>40黄色预警:创建检查点 + 复述关键规则
工具调用次数>60红色预警:拆分会话或带锚点压缩
Agent与之前决策矛盾任意验证:重新查阅真实数据源
风格/命名偏差任意复述:明确说出当前生效规则
文件读取返回意外内容任意重新读取:不要信任缓存状态
任务范围持续无限制扩大持续发生拆分:每个会话对应一个任务

Protocol

协议流程

Green Zone (0-40 tool calls)

绿色区域(0-40次工具调用)

Normal operation. No intervention needed.
正常运行,无需干预。

Yellow Zone (40-60 tool calls)

黄色区域(40-60次工具调用)

  1. CHECKPOINT - summarize progress in one paragraph
  2. RECITE - state the 3-5 most critical active rules aloud: "Active rules: [naming convention], [file structure], [error handling pattern], [testing requirement]"
  3. ASSESS - almost done? Push through. Not done? Prepare split.
  4. REDUCE - no exploratory reads. Only targeted operations.
  1. 创建检查点——用一段话总结进展
  2. 复述——大声说出3-5条最关键的当前生效规则:"当前生效规则:[命名约定]、[文件结构]、[错误处理模式]、[测试要求]"
  3. 评估——即将完成?继续推进。未完成?准备拆分会话。
  4. 精简——不进行探索性读取,仅执行针对性操作。

Red Zone (60+ tool calls OR drift signal)

红色区域(60+次工具调用或出现偏差信号)

  1. STOP - do not make more tool calls
  2. VERIFY - re-read project rules (don't trust memory)
  3. CHECKPOINT - write state to handoff document
  4. SPLIT - create handoff, suggest fresh session
  1. 停止——不再进行更多工具调用
  2. 验证——重新查阅项目规则(不要依赖记忆)
  3. 创建检查点——将状态写入交接文档
  4. 拆分——创建交接内容,建议开启新会话

Context Anchoring (anti-compaction)

上下文锚定(抗压缩)

When compaction has occurred (sudden loss of earlier context, or after /compact):
  1. RE-READ the project's rules file immediately
  2. RECITE the 3-5 critical rules aloud in your response
  3. VERIFY your planned next action matches those rules before executing
  4. If uncertain about ANY prior decision, RE-READ the source file - don't guess
当发生压缩时(突然丢失早期上下文,或执行/compact命令后):
  1. 立即重新阅读项目的规则文件
  2. 在回复中大声复述3-5条关键规则
  3. 在执行前验证计划的下一步操作是否符合这些规则
  4. 若对任何先前决策不确定,重新读取源文件——不要猜测

What survives compaction:

压缩后保留的内容:

  • Most recent user messages (high priority)
  • Currently-invoked skill body (capped at 5K tokens, oldest dropped first)
  • Git status and project structure
  • File contents read AFTER compaction
  • 最新的用户消息(高优先级)
  • 当前调用的技能主体(上限5K tokens,最早内容优先丢弃)
  • Git状态和项目结构
  • 压缩后读取的文件内容

What gets LOST in compaction:

压缩中丢失的内容:

  • Decisions made early in conversation
  • Architectural rules stated only verbally (not in files)
  • Context from tool outputs (file reads, command outputs)
  • 对话早期做出的决策
  • 仅口头说明的架构规则(未写入文件)
  • 工具输出的上下文(文件读取、命令输出)

Compaction-Safe Pattern

压缩安全模式

Keep critical instructions in FILES (CLAUDE.md, CONTEXT.md), NOT in conversation. If a rule matters, it must live in a file the agent can re-read - not in something agreed on earlier.
将关键指令保存在文件中(如CLAUDE.md、CONTEXT.md),而非对话内容里。如果某条规则很重要,必须存放在Agent可以重新读取的文件中——而不是之前口头约定的内容。

Common Mistakes

常见错误

  • Trusting that you remember the rules after 50+ tool calls (you don't - re-read)
  • Re-reading EVERYTHING to be safe (wastes tool calls - be targeted)
  • Feeling fine therefore assuming context is fine (compaction is SILENT)
  • Splitting AFTER noticing problems (split BEFORE - prevention, not recovery)
  • 工具调用超过50次后仍相信自己记得规则(你记不住——重新阅读)
  • 为了安全重新阅读所有内容(浪费工具调用——要有针对性)
  • 自我感觉良好就认为上下文没问题(压缩是悄无声息的)
  • 发现问题后才拆分会话(要提前拆分——预防而非补救)

Why This Matters

重要性

Context compaction is the #1 unsolved platform problem in 2026. Hooks don't fix it (confirmed: the agent ignores post-compaction injections because the compaction summary creates narrative momentum). This skill is the lightweight behavioral countermeasure: no infrastructure, no packages - disciplined self-monitoring that works in every harness.
上下文压缩是2026年尚未解决的头号平台问题。钩子无法解决这个问题(已证实:Agent会忽略压缩后的注入内容,因为压缩摘要会形成叙事惯性)。本技能是轻量级的行为应对方案:无需基础设施、无需安装包——严格的自我监控机制,可在任何环境中生效。