Context Recovery
Automatically recover working context after session compaction or when continuation is implied but context is missing. Works across Discord, Slack, Telegram, Signal, and other supported channels.
Use when: Session starts with truncated context, user references prior work without specifying details, or compaction indicators appear.
Safety Boundaries
- - This skill prioritizes channel/session API history to recover context.
- It does NOT perform broad filesystem scans or shell glob searches by default.
- It does NOT send recovered context to external services.
- It does NOT write to disk unless the user explicitly asks to persist recovered state.
Triggers
Automatic Triggers
- - Session begins with a
<summary> tag (compaction detected) - User message contains compaction indicators: "Summary unavailable", "context limits", "truncated"
Manual Triggers
- - User says "continue", "did this happen?", "where were we?", "what was I working on?"
- User references "the project", "the PR", "the branch", "the issue" without specifying which
- User implies prior work exists but context is unclear
- User asks "do you remember...?" or "we were working on..."
Execution Protocol
Step 1: Detect Active Channel
Extract from runtime context:
- -
channel — discord | slack | telegram | signal | etc. - INLINECODE2 — the specific channel/conversation ID
- INLINECODE3 — for threaded conversations (Slack, Discord threads)
Step 2: Fetch Channel History (Adaptive Depth)
Initial fetch:
CODEBLOCK0
Adaptive expansion logic:
- 1. Parse timestamps from returned messages
- Calculate time span: INLINECODE4
- If time span < 2 hours AND message count == limit:
- Fetch additional 50 messages (using
before parameter if supported)
- Repeat until time span ≥ 2 hours OR total messages ≥ 100
- 4. Hard cap: 100 messages maximum (token budget constraint)
Thread-aware recovery (Slack/Discord):
CODEBLOCK1
Parse for:
- - Recent user requests (what was asked)
- Recent assistant responses (what was done)
- URLs, file paths, branch names, PR numbers
- Incomplete actions (promises made but not fulfilled)
- Project identifiers and working directories
Step 3: Fetch Session Context (safe mode)
Use platform/session APIs only (no shell filesystem scans):
CODEBLOCK2
If session APIs are unavailable, skip this step and proceed with channel-only evidence.
Step 4: Optional Memory Check (explicitly scoped)
Only inspect memory if the agent runtime already provides a scoped memory tool/path.
Do not run shell glob scans across home directories.
Step 5: Synthesize Context
Compile a structured summary:
CODEBLOCK3
Step 6: Optional Persistence (consent-first)
Do not write to disk by default. If persistence is useful, ask first:
"I can cache this recovered context to memory for later continuity. Should I save it?"
Step 7: Respond with Context
Present the recovered context, then prompt:
"Context recovered. Your last request was [X]. This action [completed/did not complete]. Shall I [continue/retry/clarify]?"
Channel-Specific Notes
Discord
- - Use
channelId from the incoming message metadata - Guild channels have full history access
- Thread recovery: check for
threadId in message metadata - DMs may have limited history
Slack
- - Use
channel parameter with Slack channel ID - Thread context requires
threadId — always check for thread context first - Parent channel fetch provides surrounding conversation context
- May need workspace-level permissions for full history
Telegram / Signal / Others
- - Same
message:read interface - History depth may vary by platform
- Group vs. DM context may differ
Constraints
- - Prefer this protocol before declaring "insufficient data" when context appears missing
- Adaptive depth: start with 50, expand to 100 max
- Time-based goal: capture at least 2 hours of context when possible
- If recovery fails, state what was attempted and what data sources were unavailable
- Avoid broad filesystem scans or writes without explicit user consent
Auto-Trigger Detection
At session start, scan for compaction indicators:
CODEBLOCK4
Do not wait for user to ask — if compaction is detected, proactively recover and present context.
Example
Scenario: Session starts with compaction summary
CODEBLOCK5
Agent executes:
- 1. Detects compaction via
<summary> tag - INLINECODE12
- Calculates time span: 2.5 hours — sufficient, no expansion needed
- Parses messages, finds last action: "Now let me extract the key orchestration decisions..."
- Checks session context APIs — extracts recent user requests
- Responds:
"Context recovered. Your last request was to extract orchestration decisions from Nexus archived sessions for PR #137 (Skills & Agents System). This action did not complete before context was truncated.
Recovered state:
- - PR #137: 15,053 additions, 91 files, CONFLICTING merge state
- Spec location:
content/spec-skills-agents-system.md (2,067 lines)
Shall I proceed with the extraction?"
上下文恢复
在会话压缩后,或当需要延续但上下文缺失时,自动恢复工作上下文。适用于 Discord、Slack、Telegram、Signal 及其他支持的渠道。
使用场景:会话以截断的上下文开始、用户引用先前工作但未说明细节、或出现压缩指示符时。
安全边界
- - 本技能优先使用渠道/会话 API 历史记录来恢复上下文。
- 默认情况下,不会执行广泛的文件系统扫描或 shell 通配符搜索。
- 不会将恢复的上下文发送到外部服务。
- 除非用户明确要求持久化恢复的状态,否则不会写入磁盘。
触发条件
自动触发
- - 会话以 标签开始(检测到压缩)
- 用户消息包含压缩指示符:Summary unavailable、context limits、truncated
手动触发
- - 用户说继续、这发生过吗?、我们说到哪了?、我在做什么?
- 用户提及那个项目、那个 PR、那个分支、那个 issue但未具体说明
- 用户暗示存在先前工作但上下文不明确
- 用户问你还记得...吗?或我们之前在做...
执行协议
第 1 步:检测活跃渠道
从运行时上下文中提取:
- - channel — discord | slack | telegram | signal | 等
- channelId — 特定渠道/会话 ID
- threadId — 用于线程化会话(Slack、Discord 线程)
第 2 步:获取渠道历史记录(自适应深度)
初始获取:
message:read
channel: <检测到的渠道>
channelId: <检测到的渠道 ID>
limit: 50
自适应扩展逻辑:
- 1. 解析返回消息的时间戳
- 计算时间跨度:最新时间戳 - 最旧时间戳
- 如果时间跨度 < 2 小时且消息数量 == 限制值:
- 额外获取 50 条消息(如果支持,使用 before 参数)
- 重复直到时间跨度 ≥ 2 小时或总消息数 ≥ 100
- 4. 硬上限:最多 100 条消息(令牌预算限制)
线程感知恢复(Slack/Discord):
如果存在 threadId,首先获取线程消息
message:read
channel: <检测到的渠道>
threadId: <线程 ID>
limit: 50
然后获取父渠道以获取更广泛的上下文
message:read
channel: <检测到的渠道>
channelId: <父渠道 ID>
limit: 30
解析内容:
- - 最近的用户请求(询问了什么)
- 最近的助手响应(完成了什么)
- URL、文件路径、分支名称、PR 编号
- 未完成的操作(已承诺但未兑现)
- 项目标识符和工作目录
第 3 步:获取会话上下文(安全模式)
仅使用平台/会话 API(无 shell 文件系统扫描):
yaml
列出最近的会话(如果工具存在)
sessions_list:
limit: 5
从可能匹配的会话中拉取最后消息
sessions_history:
sessionKey: <候选会话密钥>
limit: 80
includeTools: true
如果会话 API 不可用,跳过此步骤,仅使用渠道证据继续。
第 4 步:可选的内存检查(明确限定范围)
仅当代理运行时已提供限定范围的内存工具/路径时,才检查内存。
不要在主目录上运行 shell 通配符扫描。
第 5 步:综合上下文
编译结构化摘要:
markdown
恢复的上下文
渠道: #<渠道名称>(<平台>)
时间范围: <最旧消息> 到 <最新消息>
分析的消息数: <数量>
活跃项目/任务
- - 仓库: <仓库名称>
- 分支: <分支名称>
- PR: #<编号> — <标题>
最近工作时间线
- 1. [<时间戳>] <操作/请求>
- [<时间戳>] <操作/请求>
- [<时间戳>] <操作/请求>
待处理/未完成的操作
- - ⏳ <引用的未完成操作>
- ⏳ <另一个未完成项>
最后用户请求
<引用的可能未完成的请求>
第 6 步:可选持久化(先征得同意)
默认情况下不写入磁盘。如果持久化有用,先询问:
我可以将恢复的上下文缓存到内存中以便后续延续。是否应该保存?
第 7 步:用上下文回复
呈现恢复的上下文,然后提示:
上下文已恢复。您的最后一个请求是 [X]。此操作 [已完成/未完成]。是否要 [继续/重试/澄清]?
渠道特定说明
Discord
- - 使用传入消息元数据中的 channelId
- 公会渠道具有完整的历史记录访问权限
- 线程恢复:检查消息元数据中的 threadId
- 私信可能历史记录有限
Slack
- - 使用带有 Slack 渠道 ID 的 channel 参数
- 线程上下文需要 threadId — 始终先检查线程上下文
- 父渠道获取提供周围的对话上下文
- 完整历史记录可能需要工作区级别的权限
Telegram / Signal / 其他
- - 相同的 message:read 接口
- 历史记录深度可能因平台而异
- 群组与私信上下文可能不同
约束条件
- - 在上下文缺失时,优先使用此协议,然后再声明数据不足
- 自适应深度:从 50 条开始,最多扩展到 100 条
- 基于时间的目标:尽可能捕获至少 2 小时的上下文
- 如果恢复失败,说明尝试了什么以及哪些数据源不可用
- 未经用户明确同意,避免广泛的文件系统扫描或写入
自动触发检测
在会话开始时,扫描压缩指示符:
python
触发检测的伪代码
if 消息包含
:
triggercontextrecovery()
elif 消息包含 [Summary unavailable, context limits, truncated, compacted] 中的任意一个:
triggercontextrecovery()
elif 消息匹配延续模式:
# 继续、我们说到哪了、这发生过吗等
triggercontextrecovery()
不要等待用户询问 — 如果检测到压缩,主动恢复并呈现上下文。
示例
场景: 会话以压缩摘要开始
用户消息:由于上下文限制,摘要不可用...
用户消息:这发生过吗?
代理执行:
- 1. 通过 标签检测到压缩
- message:read channel=discord channelId=1460342116516364533 limit=50
- 计算时间跨度:2.5 小时 — 足够,无需扩展
- 解析消息,找到最后操作:现在让我提取关键编排决策...
- 检查会话上下文 API — 提取最近的用户请求
- 回复:
上下文已恢复。您的最后一个请求是从 Nexus 存档会话中提取 PR #137(技能与代理系统)的编排决策。此操作在上下文被截断前未完成。
恢复的状态:
- - PR #137:15,053 行新增,91 个文件,合并状态冲突
- 规范位置:content/spec-skills-agents-system.md(2,067 行)
是否继续执行提取?