agentscope-ai / agentscope-ai/QwenPaw

[Feature]: Agent self-managed context lifecycle - auto checkpoint & reset for cron tasks

Open
#4,525 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34.9k
Forks
3.1k
Avg merge
1d 15h
Merged PRs (30d)
225

Description

## Summary Agents running automated workflows (cron tasks, long multi-step pipelines) gradually lose execution quality as conversation context grows, even with automatic compaction. The model's instruction-following and rule compliance degrade noticeably at 50-60% context utilization, leading to skipped steps, ignored rules, and inconsistent behavior. ## Problem Currently in v1.1.7: - **Auto-compaction exists** (LightContextManager at 70% threshold) but compressed context still degrades execution quality - **`/compact` and `/clear` exist** as manual commands, but agents cannot invoke them programmatically - **Cron executor creates fixed session IDs** (even with `share_session=False`), so repeated cron triggers accumulate messages in the same session - **No API for agents to detect their own context usage** - we can see prompt_tokens after each LLM call via `TokenRecordingModelWrapper._usage_by_session`, but agents have no tool or endpoint to query this ## Proposed Solution Add **agent self-managed context lifecycle** with two capabilities: ### 1. Context usage visibility Expose current context token usage to the agent as a tool or system-level metadata: ```python # Option A: MCP tool get_context_usage() -> { "prompt_tokens": 45000, "max_input_length": 128000, "usage_ratio": 0.35, "message_count": 47 } # Option B: Inject into system prompt context # Automatically update a context health indicator in the system prompt ``` Related: PR #4465 (Cache context token estimates from model usage) — this would provide the foundation. ### 2. Auto-reset session for cron tasks Add a new runtime option for cron jobs: ```python class JobRuntimeSpec(BaseModel): # ... existing fields ... auto_reset: bool = Field( default=False, description="When True, each cron trigger creates a fresh session. Previous session messages are checkpointed before reset." ) reset_checkpoint_dir: str = Field( default="checkpoints", description="Directory to store pre-reset checkpoints." ) ``` When `auto_reset=True`: 1. Before each cron trigger, save a checkpoint of the current session state 2. Create a new session_id (e.g., `cron:{job.id}:{run_id}`) 3. Inject checkpoint summary into the new session's first message 4. Agent starts fresh with full execution quality ### 3. Programmatic `/clear` for agents Allow agents to trigger `/clear` on their own session via a tool call when they detect degradation: ```python # As an MCP tool available to the agent reset_my_context(checkpoint_file: str = "") -> { "status": "reset", "checkpoint_saved": "memory/checkpoints/2026-05-19-143000.md" } ``` ## Use Case We run multi-step development workflows where: 1. Cron triggers agent every N minutes 2. Agent reads checkpoint, dispatches task to sub-agents, validates results, commits code 3. Over hours of repeated triggers, the agent session accumulates 50K+ tokens 4. Agent starts skipping commit gates, ignoring self-improving rules, making mistakes it wouldn't make in a fresh session With this feature, each cron trigger would start with a clean context, and the agent would maintain quality throughout hours of automated operation. ## Related - Issue #4463 (Improve context token estimation) - PR #4465 (Cache context token estimates from model usage) - Issue #4024 (Hermes self-evolving mechanism) ## Environment - QwenPaw v1.1.7 - Model: GLM-5.1 (128K context) - OS: Windows 10

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.