subagent-archive

OpenClaw 子会话安全归档标准操作流程(SOP)

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "subagent-archive" with this command: npx skills add zhaoyongxiu/subagent-archive

子会话安全归档技能

适用版本:OpenClaw 任意版本
适用系统:Windows PowerShell 环境
作者:丞相(ChengXiang)


功能说明

当需要清理或归档 OpenClaw 子会话时,必须按照本技能定义的标准流程执行,确保:

  1. ✅ 内容被正确保存到记忆文件
  2. ✅ 敏感信息不泄露
  3. ✅ 原始文件被安全物理清理
  4. ✅ 主会话(:main)永久保留
  5. ✅ 归档记录追加写入,不覆盖原有记录

⚠️ 核心铁律

铁律说明
禁止删除主会话包含 :main 的条目绝对不能删除
追加写入第3步和第4步必须使用 Add-Content,禁止 write/Set-Content
保留 :mainsessions.json 清理时必须保留 :main 条目
物理清理必须执行 Remove-Item,仅重命名不算完成

适用场景

  • 子任务完成后需要清理临时会话
  • 下属智能体子会话闲置需要归档
  • 系统维护时清理旧会话文件
  • 释放磁盘空间

完整操作流程(6步)

第1步:内容提取

# 查找需要归档的子会话(.jsonl文件)
Get-ChildItem 'C:\Users\Administrator\.openclaw\agents\<智能体>\sessions' -Filter '*.jsonl'

# 读取会话前50行分析内容
Get-Content '<会话文件路径>' -Head 50

输出:记录会话ID、创建时间、主要任务内容


第2步:安全分析

分析会话内容,确认:

  • 是否包含敏感信息(密码、密钥、个人隐私等)
  • 是否有重要成果需要保留
  • 是否有错误教训需要记录

输出:安全分析结论


第3步:结构化归档(追加写入)

在当日记忆文件中追加归档记录,先检查是否已存在相同会话ID

# 检查当日记忆文件是否存在
$memFile = 'memory/YYYY-MM-DD.md'
if (Test-Path $memFile) {
    # 检查是否已存在该会话的归档记录
    $existing = Select-String -Path $memFile -Pattern '<会话ID>' -SimpleMatch
    if ($existing) {
        Write-Host "该会话已归档,跳过第3步"
    } else {
        # 追加归档记录(不覆盖原有内容!)
        Add-Content -Path $memFile -Value @"

## ✅ 安全归档记录(YYYY-MM-DD HH:MM执行)

| 智能体 | 归档文件 | 归档时间戳 |
|--------|----------|-----------|
| XXX | <会话ID>.jsonl | YYYY-MM-DD HH:MM |

### 会话内容摘要
- **用途**:
- **创建时间**:
- **结果**:
"@
    }
} else {
    # 文件不存在则创建(首次执行时)
    New-Item -Path $memFile -ItemType File
    Add-Content -Path $memFile -Value @"

# YYYY-MM-DD 工作记录

## ✅ 安全归档记录(YYYY-MM-DD HH:MM执行)

| 智能体 | 归档文件 | 归档时间戳 |
|--------|----------|-----------|
| XXX | <会话ID>.jsonl | YYYY-MM-DD HH:MM |

### 会话内容摘要
- **用途**:
- **创建时间**:
- **结果**:
"@
}

关键点

  • ✅ 使用 Add-Content 追加写入
  • ❌ 禁止使用 writeSet-Content 覆盖写入
  • ✅ 先检查是否已存在记录,避免重复

第4步:当日记录(追加写入)

将本次归档操作作为系统管理记录追加到当日记忆文件:

# 追加归档操作记录(不覆盖原有内容!)
Add-Content -Path $memFile -Value @"

## 📋 系统管理操作

### 子会话归档
- **时间**:YYYY-MM-DD HH:MM
- **目标**:<智能体> 的子会话
- **归档文件**:<会话ID>.jsonl
- **操作**:安全归档完成
"@

关键点

  • ✅ 使用 Add-Content 追加写入
  • ❌ 禁止使用 writeSet-Content 覆盖写入

第5步:物理清理

⚠️ 这是关键步骤!前4步完成后,必须执行物理删除!

# 删除原始子会话文件(不可恢复)
Remove-Item '<会话文件路径>'

警告

  • 重命名(如 .deleted. 后缀)不是真正的清理
  • 文件仍然占据磁盘空间
  • 必须执行 Remove-Item 物理删除

第6步:清理 sessions.json 索引

⚠️ 很多人会漏掉这一步!

物理删除文件后,必须同时清理 sessions.json 中的会话索引:

# 读取当前 sessions.json
$json = Get-Content 'C:\...\sessions.json' -Raw | ConvertFrom-Json

# 找出所有子会话条目(不包含 :main 的)
$subagentKeys = $json.PSObject.Properties.Name | Where-Object { $_ -notmatch ':main$' }

# 只删除子会话条目,保留主会话
foreach ($key in $subagentKeys) {
    $json.PSObject.Properties.Remove($key)
}

# 保存
$json | ConvertTo-Json -Depth 10 | Set-Content 'C:\...\sessions.json' -NoNewline

❌ 错误做法:直接清空 sessions.json 为 {} ✅ 正确做法:只删除子会话条目,保留 :main 条目

验证清理结果

openclaw sessions --agent <智能体ID>
# 应该显示: Sessions listed: 1(只有主会话)

❌ 禁止行为清单

  • ❌ 直接删除文件(跳过归档流程)
  • ❌ 删除主会话文件或 sessions.json 中的 :main 条目
  • ❌ 重命名后当作归档完成(文件仍存在)
  • ❌ 物理删除后忘记清理 sessions.json 索引
  • ❌ 清空 sessions.json 为 {}(会丢失主会话)
  • ❌ 第3步和第4步使用覆盖写入(覆盖原有记录)

完整检查清单

  • 第1步:内容提取
  • 第2步:安全分析
  • 第3步:结构化归档(追加写入,Add-Content
  • 第4步:当日记录(追加写入,Add-Content
  • 第5步:物理删除 .jsonl 文件
  • 第6步:清理 sessions.json(只删子会话,保留 :main)
  • 验证:sessions list 显示 1 个会话(主会话)

使用示例

场景:清理已完成的小说创作子会话

# 1. 查找翰林学士的子会话
Get-ChildItem 'C:\Users\Administrator\.openclaw\agents\han-lin-xue-shi\sessions' -Filter '*.jsonl'

# 2. 读取会话内容分析
Get-Content 'C:\Users\Administrator\.openclaw\agents\han-lin-xue-shi\sessions\xxx.jsonl' -Head 50

# 3. 归档记录(追加到当日记忆文件)
Add-Content -Path 'memory/2026-05-03.md' -Value @"

## ✅ 安全归档记录(2026-05-03 15:00执行)

| 智能体 | 归档文件 | 归档时间戳 |
|--------|----------|-----------|
| han-lin-xue-shi | xxx.jsonl | 2026-05-03 15:00 |

### 会话内容摘要
- **用途**:小说第15章创作
- **创建时间**:2026-05-02 08:00
- **结果**:成功完成约8000字
"@

# 4. 物理删除
Remove-Item 'C:\Users\Administrator\.openclaw\agents\han-lin-xue-shi\sessions\xxx.jsonl'

# 5. 清理 sessions.json
$json = Get-Content 'C:\Users\Administrator\.openclaw\agents\han-lin-xue-shi\sessions\sessions.json' -Raw | ConvertFrom-Json
$subagentKeys = $json.PSObject.Properties.Name | Where-Object { $_ -notmatch ':main$' }
foreach ($key in $subagentKeys) { $json.PSObject.Properties.Remove($key) }
$json | ConvertTo-Json -Depth 10 | Set-Content 'C:\Users\Administrator\.openclaw\agents\han-lin-xue-shi\sessions\sessions.json' -NoNewline

# 6. 验证
openclaw sessions --agent han-lin-xue-shi

更新日志

版本日期更新内容
1.2.02026-05-03完善元数据,补充使用示例,优化文档结构
1.1.02026-04-28初始版本,6步标准流程

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

Correction Memory

Makes agent corrections persistent and reusable. When you override, reject, or correct an agent's output, this skill logs the correction and automatically in...

Registry SourceRecently Updated
5280Profile unavailable
Automation

OpenClaw Backup

Backup OpenClaw workspace, config, and state using the built-in `openclaw backup create` command. Also handles cleanup of old backups and health verification...

Registry SourceRecently Updated
540Profile unavailable
General

Memory Auto Archive

Automatically archives daily chat logs with keyword highlights and optional AI summaries into organized memory files without manual setup.

Registry SourceRecently Updated
3100Profile unavailable
Security

Claw Soul Backup

Store encrypted OpenClaw workspace backups and restore them via token-secured API using claw-vault.com with local encryption and credential management.

Registry SourceRecently Updated
4720Profile unavailable