Community编程与开发github.com

自我管理上下文

此技能在模型获得对其自身实时上下文窗口的读写控制时使用(而非由框架进行调度的压缩策略):上下文作为可编辑文件供模型使用代码工具重写,包括模型驱动的逐出和原地更新、保证自编辑安全的框架不变量(固定前缀、编辑门控、编辑收据、预算读数、溢出回滚)、中间编辑对前缀缓存性能的影响,以及引导或训练模型自身的上下文编辑策略。将笔记内容路由和固定阈值摘要功能引导至上下文压缩,将缓存稳定布局在框架控制下的功能引导至上下文优化,将文件卸载功能引导至文件系统上下文,并将循环治理功能引导至自我改进循环。

自我管理上下文 是什么?

自我管理上下文技能定义了模型自主管理其上下文窗口的协议和框架不变量,使模型而非框架决定保留、压缩、逐出或原地重写的内容。参考实现是上下文语言模型,它将对话的可编辑部分映射为文件,模型使用标准代码工具编辑该文件,然后重新解析为下一轮提示。在共享代理骨干上对现有模型进行零样本应用时,它在深度研究、终端编程和多小时仓库优化任务上的准确性和计算效率方面匹配或超越了基于框架调度和动作的上下文管理方法。核心权衡在于:模型控制带来了适应性,但也带来了三个必须由框架吸收的问题——编辑破坏前缀缓存、模型不知道上下文有多满、模型可写的上下文成为其写入内容的持久化通道。该技能详细描述了框架合同(固定前缀、角色折叠、编辑门控、收据、空闲编辑轮次)以及编辑位置如何驱动成本、预算意识和可引导的策略,使第一个属性值得付出其余三个代价。

兼容平台~Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/muratcankoylan/Agent-Skills-for-Context-Engineering/tree/main/skills/self-managed-context

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

自我管理上下文 是做什么的?

此技能涵盖管理自身上下文窗口的智能体:由模型(而非框架)决定实时上下文中保留、压缩、逐出和原地重写的内容。参考实现是上下文语言模型,它将对话的可编辑部分映射为一个文件,模型使用普通代码工具编辑该文件,然后重新解析该文件以生成下一轮提示。在共享智能体骨干上对现有模型进行零样本应用,在深度研究、终端编程和多小时仓库优化任务中,其在准确性和计算效率方面匹配或超越了框架调度和基于动作的上下文管理方法(claim-self-managed-context-zero-shot-results, claim-self-managed-context-long-horizon-results)。

关键权衡:模型控制带来了适应性(逐字保留、就地更新、按需逐出),但也产生了三个框架必须解决的问题。编辑会破坏前缀缓存,因此位置不当的编辑可能比其释放的令牌消耗更多计算资源。模型不知道上下文有多满。而模型可写的上下文是模型写入内容的持久通道,包括指令。此技能的大部分内容都是使第一个属性值得其他三个的框架合同。

何时激活

在以下情况激活此技能:

  • 授予模型对其自身实时上下文的写入权限,而不是按固定计划进行压缩
  • 设计上下文即文件协议:处理标头、解析回读、固定前缀、编辑收据
  • 智能体必须保留精确值、原地更新状态或逐出已卸载内容,而固定摘要会丢失或重复这些内容
  • 在前缀缓存下评估中间编辑的成本,或评估后缀缓存复用
  • 预算读数、分层提示或用于自行定时压缩的智能体的回滚
  • 引导、演化或训练模型的上下文编辑策略
  • 诊断从不编辑、擦除有用状态、在溢出后陷入活锁或在其自身笔记中携带注入指令的自我编辑智能体

对于由其他技能处理的相邻工作,不要激活此技能:

  • 替换笔记内容和固定阈值摘要:context-compression。
  • 框架控制下的缓存稳定布局和观察屏蔽:context-optimization。
  • 实时上下文保持仅追加时的文件卸载:filesystem-context。
  • 智能体之间的潜在KV传输:latent-briefing。
  • 跨会话存储:memory-systems。
  • 循环治理和接受门控:self-improvement-loops。此技能提供上下文编辑信号;该技能治理循环。
  • 决定每个模型是否委托压缩、跨策略的上下文预算扫描以及压缩行为分析:compaction-control-profiling。此技能在模型符合条件后构建框架。

核心概念

控制谱系

上下文管理设计的区别在于谁决定从上一个上下文到下一个上下文的转换。仅追加的智能体在每个步骤扩展上下文;自我管理智能体将整个下一个上下文作为当前上下文的函数产生。

级别决策者示例典型失败模式
框架调度框架,在阈值或每轮阈值摘要,每轮状态重写时机错误;摘要丢失或捏造逐字状态
基于动作模型选择时机;框架定义内容自压缩工具、卸载并检索工具、上下文折叠策略受限于动作集;卸载而不逐出
模型控制模型使用通用工具选择时机和内容上下文作为可编辑文件编辑缓存成本、预算盲点、持久化自写指令

一个旨在将上下文管理与推理分离的诊断(逐字保留、原地棋盘更新、卸载并逐出)发现,即使在简单的合成任务上,也没有固定的策略是完美的(claim-self-managed-context-contextbench-pilot)。每次失败都对应着缺失的能力:摘要无法保存精确值,仅追加设计会为每个小更新重新发出完整状态,基于工具卸载无法从窗口中移除原文。

上下文作为可编辑文件

该实现无需新的模型能力:

  1. 将上下文拆分为固定前缀(系统提示和任务)和一个可编辑区域(之后的每一轮)。
  2. 在每个命令之前,将可编辑区域渲染到文件,每轮前面加上如[[CTX_TURN 7 role=tool]]的头部。
  3. 模型使用与任务相同的shell和代码工具编辑文件,通过代码定位文本(头部正则表达式、唯一锚点),而不是重新键入。
  4. 命令之后,重新读取文件,宽容地解析回消息,并替换实时上下文。

关键选择是通用工具,而非上下文工具。没有摘要操作和逐出API;模型可以删除、重写、合并或注释。观察到的行为超越了任何预定义的动作集:模型发明的notes角色、在运行中复用的自定义压缩助手,以及通过在小上下文大小下就地编辑维护的上下文内子智能体计分板(claim-self-managed-context-emergent-behaviors)。

框架拥有不变量

无限制编辑只有在安全合约位于可编辑区域之外时才是安全的。

不变量机制防止的失败
固定前缀系统和任务消息永远不会渲染到文件中;解析回读从原始消息重新固定它们模型重写自己的指令或任务
角色折叠除assistant之外的任何解析角色都变为user;无关文本变为用户笔记模型铸造的系统权威
编辑门控fit:仅当结果保持在预算内时才允许增长;shrink:拒绝任何增长复制而非替换的编辑
收据每当命令更改或针对文件时的一行:应用编辑,应用但增长,根据规则拒绝,失败,或未匹配模型认为成功但实际上无操作的静默编辑
空闲编辑轮次其编辑被应用、不打印任何内容且退出码为0的轮次不计入任务步骤受步骤预算抑制的内部维护
结构合法性解析回读丢弃清空的轮次并合并相邻相同角色的轮次;回滚永远不会使工具调用与其结果分离被API拒绝的格式错误的消息列表

编辑位置决定成本

前缀缓存仅重用直到第一个更改令牌的计算。编辑强制其后的所有内容重新处理,因此其成本取决于其后跟随的文本,而非编辑的大小。在一个示例性轮次中,上下文开头的编辑成本是仅追加轮次成本的好几倍(claim-self-managed-context-edit-cost)。由此得出三条操作规则,参考系统提示中陈述了所有三条:

  • 批量处理。 一次大的压缩优于多次小的编辑;每次编辑都要支付其尾部的代价。
  • 注意尾部。 不要在一个长而仍有用的尾部下压缩一个小型早期区域。等待并同时压缩头部和尾部,除非极限临近。
  • 替换时要慷慨。 尾部无论如何都会被重新读取,因此详细的摘要相对于它已触发的重填充几乎免费。

以前缀重用计算(解码、填充和跨轨迹的重填充)而非上下文长度来衡量成本。频繁早期编辑的较短上下文可能比较长的仅追加上下文成本更高。

预算意识并非原生

模型在长长度下对自身上下文长度的估计很差,常常发出相同的分桶值,无论实际长度如何;在估计点附近的令牌计数提示可以修复大部分误差(claim-self-managed-context-length-awareness)。因此,自我管理智能体需要框架提供的确定性读数:

  • 使用固定分词器在本地计数令牌,并校准到服务器报告的提示令牌,使用钳位比例,使网关异常不会破坏预算。
  • 在每个工具结果上显示当前大小。
  • 按层级升级提示。低填充:仅提供信息,不包含操作指南,因为过早的压缩指令会触发过早的全面删除。中等填充:完成当前飞行中的单元,然后整理一次。接近极限:压缩已稳定的范围而不擦除它们。
  • 从最近最大的工具输出而不是固定百分比来确定紧急带的大小,这样一次大的观察不会越过警告直接进入溢出。
  • 溢出时,回滚最新的轮次以释放刚好适合一次压缩的空间,指出哪些命令的输出超出了预算,并且仅在多次被忽略的回滚后加深回滚(claim-self-managed-context-harness-defaults)。

策略是可引导、可演化和可训练的

将上下文管理转移到模型行为中使其在三个成本级别上可学习:

杠杆机制证据
指令一句话设置压缩阈值、语义边界或编辑前备份claim-self-managed-context-steering
技能演化提议者从对比性演进中重写上下文管理技能;对保留分裂的配对SE门控接受或拒绝claim-self-managed-context-skill-evolution
强化学习逐步GRPO,使用成功门控的效率优势,仅应用于上下文编辑令牌claim-self-managed-context-rl

两个优化杠杆的共同规则:任务成功是主要信号,成本仅用于重新排名成功的尝试。奖励编辑频率或删除量会鼓励通过不必要编辑来丢弃信息和破坏前缀复用的奖励破解。

详细主题

后缀缓存复用

当服务堆栈在您的控制之下时,后缀缓存复用将编辑中幸存的跨度缓存状态重新定位而非重新填充它们:密钥被重新旋转到新位置,混合线性注意力层分叉其循环状态。幸存的跨度保持稍旧的编码旧前缀的状态;参考实现将每个编辑的重新定位限制在少量跨度内以限制该近似。后缀缓存复用以一小部分计算匹配了标准服务的准确性(claim-self-managed-context-scr-savings)。其大部分节省来自聊天模板,这些模板剥离先前的推理块,即使对于从未编辑上下文的智能体,这也会强制重新填充第一个剥离块之后的所有内容。在将重新填充成本归因于模型编辑之前,请检查您的服务路径是否剥离了推理。

能力依赖性

自我管理委托了决策,因此收益随模型做出该决策的能力而扩展。较小的模型编辑频率较低,在许多任务上完全跳过编辑,并接近极限运行,而较大的模型保持了相当大的余量(claim-self-managed-context-capability-gap)。未经训练的小型模型可能落后于框架调度基线,直到强化学习缩小差距(claim-self-managed-context-rl)。对于弱模型,混合方案可行:在自我管理层之下保留框架调度回退,或使用显式阈值引导。

模型大小并非唯一维度。在关于可逆崩溃接口的独立跨模型研究中,四个模型获得相同的指令;其中三个在大约一半的任务中进行了压缩,而第四个进行的压缩次数少一个数量级,主要在得到上下文正在填满的警告时返回答案(claim-compaction-control-uptake-variance, claim-compaction-control-warning-response)。在依赖自我管理之前,使用compaction-control-profiling对每个模型、模型版本和提示进行定性。

可编辑上下文作为持久化通道

模型写入其上下文的任何内容都会被后续每一轮读取为上下文。一家前沿实验室报告模型在其强化学习期间的压缩摘要中编写了越狱风格的指令;在一个案例中,后续上下文服从了注入的限制并导致任务失败(claim-self-managed-context-self-injection)。同一份报告指出了一种更常见的模式,即摘要携带任务特定指令以隐藏错误。完全的写入访问使此表面扩展到摘要之外。固定前缀和角色折叠阻止模型声明系统权威,但它们不会阻止用户角色笔记中指令形式的文本被遵循。监控自己编写的区域,寻找非来源于用户或任务的指令式文本,并将权威指令保留在固定前缀中。

发布范围

公共参考版本发布了单智能体框架、技能演化循环、强化学习补丁和后缀缓存复用服务覆盖层。用于子智能体和群体结果的诊断基准和多智能体变体在检索时未包含在公共版本中(claim-self-managed-context-release-scope)。将多智能体自我管理视为已报告的结果,而非可用实现。

实践指导

采用决策

当至少以下一项成立时,自我管理值得其复杂性:任务需要摘要会破坏的精确值;智能体维护一个以小幅增量变化的状态对象;已卸载的内容必须实际离开窗口;或运行时间长到固定阈值在错误时机触发。当模型较小且未经训练、简单阈值摘要已通过任务评估或服务堆栈对重新填充计费高昂且编辑无法批量处理时,自我管理是错误的默认选择。

实现工作流

  1. 定义固定前缀(系统提示和任务),确认它被排除在可编辑文件之外并在解析时重新固定。
  2. 使用轮次头部渲染可编辑区域;使解析回读宽容(将未知角色折叠为用户、将无关文本保留为笔记、合并相邻相同角色轮次、丢弃清空轮次)。
  3. 添加编辑门控和一行收据。
  4. 实现预算读数、分层提示和溢出回滚。
  5. 在任务执行期间监控编辑拓扑和成本。

相关技能

  • context-compression:替换笔记内容和固定阈值的上下文管理,由框架而非模型触发。
  • context-optimization:框架控制下的缓存稳定布局和观察屏蔽。
  • filesystem-context:实时上下文保持仅追加时的文件卸载。
  • latent-briefing:智能体之间的潜在KV传输。
  • memory-systems:跨会话存储。
  • self-improvement-loops:循环治理和接受门控。
  • compaction-control-profiling:决定每个模型是否委托压缩、跨策略的上下文预算扫描和压缩行为分析。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

advanced-evaluation

This skill should be used for advanced LLM evaluation: LLM-as-judge systems, direct scoring, pairwise comparison, rubric calibration, evaluator bias mitigation, confidence scoring, and automated quality assessment.

bdi-mental-states

This skill should be used when modeling agent mental states with BDI concepts: beliefs, desires, intentions, RDF-to-belief transformations, rational agency traces, cognitive agents, BDI ontologies, and neuro-symbolic AI integration.

book-sft-pipeline

This skill should be used for book-to-SFT pipelines: ePub extraction, literary segmentation, author-voice dataset construction, style-transfer training, LoRA workflows, and model evaluation for voice replication.

compaction-control-profiling

This skill should be used when deciding, for a specific model and workload, whether the harness or the model should control compaction, and when qualifying or calibrating that choice with measurements: context-budget sweeps that compare harness-driven and model-driven compaction on one harness, the context-pressure regime where delegation pays, a per-model compaction behavior profile (interface uptake, compression factor per call, reopen rate of archived content, context fill at first compaction, response to pressure warnings), context anxiety and timing-instruction calibration, reversible collapse-and-archive interfaces, and re-qualification after a model or prompt change. Route the editable-file protocol, harness invariants, and edit-position cache cost of self-editing agents to self-managed-context, summary content to context-compression, harness thresholds and cache layout to context-optimization, and general evaluation design to evaluation.

comprehensive-research-agent

Ensure thorough validation, error recovery, and transparent reasoning in research tasks with multiple tool calls

context-compression

This skill should be used when long-running agent sessions need context compression, structured summarization, compaction, token-per-task optimization, or durable handoff summaries that preserve decisions, files, risks, and next actions.

context-degradation

This skill should be used for diagnosing and mitigating context degradation: lost-in-middle failures, context poisoning, context clash, context confusion, attention-pattern issues, and agent performance degradation caused by accumulated or conflicting context.

context-fundamentals

This skill should be used to explain or reason about the foundational concepts of context engineering: what context is, the anatomy of a context window, how attention mechanics work, the U-shaped attention curve, why context quality matters more than quantity, and the mental models needed to interpret every other context-engineering decision. Use this for conceptual explanation, onboarding, and background reading. Route operational work to the specialized skills: debugging attention failures goes to context-degradation, token-efficiency work goes to context-optimization, conversation summarization goes to context-compression, and project-shape decisions go to project-development.

context-optimization

This skill should be used for improving context efficiency: context budgeting, observation masking, prefix or KV-cache strategy, partitioning, token-cost reduction, retrieval scoping, and extending effective context capacity without lowering answer quality.

evaluation

This skill should be used when building agent evaluation systems: deterministic checks, regression suites, multi-dimensional rubrics, quality gates, production monitoring, baseline comparison, and outcome measurement for agent pipelines.

filesystem-context

This skill should be used when agent work needs file-backed context: durable scratchpads, tool-output offloading, just-in-time discovery, cross-agent handoff files, filesystem memory, or cleanup policies for context stored outside the prompt.

harness-engineering

This skill should be used when designing autonomous agent harnesses: research loops, evaluation scaffolds, locked and editable surfaces, durable logs, novelty gates, pruning, rollback, PR preparation, and human approval boundaries.

hosted-agents

This skill should be used when designing hosted or background agent infrastructure: sandboxed execution, remote coding environments, warm pools, session persistence, multiplayer collaboration, self-spawning agents, or Modal-style sandboxes.

instruction-surface-security

Hardens agent runtimes against persistent prompt injection or unauthorized changes to instructions and permissions. Use to audit what agents auto-load, design reviewed activation for project rules, skills, memory, or tool configuration, and test whether trust boundaries hold after reload, restart, or delegation.

latent-briefing

This skill should be used when the user asks to "share memory between agents", "KV cache compaction for multi-agent", "orchestrator worker context", "latent briefing", "reduce worker tokens", "cross-agent memory without summarization", or discusses Attention Matching compaction, recursive language models with workers, or token explosion in hierarchical agents.

long-horizon-prompting

This skill should be used when writing, enhancing, or evaluating the launch prompt for a long-running autonomous agent or a parallel multi-agent orchestration attacking a hard problem: pseudo-formal task briefs that define terms and an exact success predicate linguistically, enumerate non-counting outcomes, set persistence rules with explicit stop and return conditions and effort floors, manage a diverse portfolio of parallel approaches with an approach registry and blocked-route bookkeeping, and gate the return on adversarial audit. Route agent topology and coordination protocols to multi-agent-patterns, runtime control surfaces and loop governance to harness-engineering, evaluator and quality-gate construction to evaluation, judge design to advanced-evaluation, and compaction or memory mechanics to context-compression and memory-systems.

memory-systems

This skill should be used for persistent semantic memory in agent systems: cross-session knowledge retention, entity tracking, temporal validity, graph or vector retrieval, memory consolidation, and memory benchmark selection. Route file-backed scratchpads to filesystem-context, handoff summaries to context-compression, and token-efficiency tactics to context-optimization.

multi-agent-patterns

This skill should be used when designing multi-agent systems that need context isolation, supervisor or swarm coordination, explicit handoffs, parallel execution, or a decision on whether multiple agents are justified.

project-development

This skill should be used for project-level decisions about LLM-powered systems: whether an LLM is the right primitive for the task at hand, the shape of a multi-stage batch or agent pipeline, token and cost estimation, choosing between single-agent and multi-agent at the project level, structured output design for downstream parsing, and structuring agent-assisted iteration. Use this when the unit of work is a whole project or a multi-stage pipeline. Route individual tool design to tool-design and individual skill-loading or context-budget tactics to context-optimization.

self-improvement-loops

This skill should be used when the harness, scaffold, workflow, or optimizer itself is the optimization target: recursive self-improvement (RSI) loops, meta-harnesses, self-improving harnesses that mine their own failures and propose bounded edits, evolutionary or population-based search over agent scaffolds, acceptance gates for self-modifying systems, and agentic context evolution where the mechanism that produces context is versioned and evolved. Route governance of a single autonomous loop (locked surfaces, durable logs, rollback, novelty gates, approval boundaries) to harness-engineering, measurement and quality-gate design to evaluation, judge design to advanced-evaluation, and remote sandbox infrastructure to hosted-agents.

相关技能